Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This question has been closed for a while, but I recently solved this problem and thought people might wish to see how I ended up solving it.</p> <p>Doing it this way, the print statements are actually printed to the screen as soon as the list of expressions descended from it are finished being evaluated:</p> <pre><code>module IOInterpreter where import Data.Char ------------------------------------------------------------------------------- type Id = [Char] type Output = [Char] type Value = Int type Table = [(Id, Value)] data Stm = CompoundStm Stm Stm | AssignStm Id Exp | PrintStm ExpList deriving Show data Exp = IdExp Id | NumExp Value | OpExp Exp Op Exp | EseqExp Stm Exp deriving Show type ExpList = [Exp] data Op = Plus | Minus | Times | Div deriving Show ------------------------------------------------------------------------------- example :: Stm example = CompoundStm (AssignStm "a" (OpExp (NumExp 5) Plus (NumExp 3))) (CompoundStm (AssignStm "b" (EseqExp (PrintStm [IdExp "a", OpExp (IdExp "a") Minus (NumExp 1)]) (OpExp (NumExp 10) Times (IdExp "a")))) (PrintStm [IdExp "b"])) ------------------------------------------------------------------------------- tableUpdate :: Table -&gt; Id -&gt; Value -&gt; Table tableUpdate t i v = (i,v):t tableLookup :: Table -&gt; Id -&gt; Value tableLookup ((x,v):t) i | x == i = v tableLookup ((x,v):t) i | x /= i = tableLookup t i ------------------------------------------------------------------------------- execute :: Stm -&gt; IO() execute s = do interpStm s [] return () interpStm :: Stm -&gt; Table -&gt; IO Table interpStm (CompoundStm s1 s2) t = do t' &lt;- interpStm s1 t interpStm s2 t' interpStm (AssignStm i e) t = do (v, t') &lt;- interpExp e t return $ tableUpdate t' i v interpStm (PrintStm es) t = do (s, t') &lt;- interpExpList es t putStrLn s return t' interpExp :: Exp -&gt; Table -&gt; IO (Value, Table) interpExp (NumExp v) t = return (v, t) interpExp (IdExp i) t = return (tableLookup t i, t) interpExp (EseqExp s e) t = do t' &lt;- interpStm s t interpExp e t' interpExp (OpExp e1 o e2) t = do (v1, t') &lt;- interpExp e1 t (v2, t'') &lt;- interpExp e2 t' return (f o v1 v2, t'') where f Plus v1 v2 = v1 + v2 f Minus v1 v2 = v1 - v2 f Times v1 v2 = v1 * v2 f Div v1 v2 = v1 `div` v2 interpExpList :: ExpList -&gt; Table -&gt; IO (String, Table) interpExpList [] t = return ("", t) interpExpList (e:es) t = do (v, t') &lt;- interpExp e t (s, t'') &lt;- interpExpList es t' return $ (show v ++ " " ++ s, t'') </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload