<div>Hi ,</div>
<div> With help I got the following to print out each step in the evaluation of the add. However I was just wondering if it was possible to make the evaluation go back one step at a time. I tried to use a get count function but this doesnt seem so easy. </div>
<div> </div>
<div>data Expression = Val Integer<br> | Add Expression Expression<br> | Subtract Expression Expression<br> | Multiply Expression Expression<br> | Divide Expression Expression<br>
deriving Show<br>demo1 = (Add(Add(Add(Add(Val 6)(Val 5))(Val 10))(Val 7))(Val 30))</div>
<div>evalStep :: Expression -> Expression<br>evalStep (Val x)= (Val x) </div>
<div>evalStep (Add x y)<br> = case x of<br> (Val a) -> case y of<br> (Val b) -> Val (a+b)<br> left -> Add x (evalStep y)<br> right -> Add (evalStep x)y <br>
<br>evaluate :: Expression -> IO ()</div>
<div>-- Base case<br>evaluate (Val a) = return ()</div>
<div><br>-- Recursive case<br>evaluate e = do<br> putStrLn "Evaluating one more step"<br> let e' = (evalStep e)<br> putStrLn ("Result is "++(show e'))<br> putStrLn "Do another step (y/n)? :"<br>
c <- getLine<br> if (c=="y")then<br> evaluate e'<br> else putStrLn("Ok you said" ++ show[c] ++ "so that's it" ++ show getCount)<br> <br>getCount:: Expression -> Int<br>
getCount e' = n, n=1<br> <br> if(getCount == 1) then <br> putStrLn ("Cannot go back")<br> else putStrLn ("One step back" ++ show n-1)</div>
<div> </div>
<div>All thoughts welcome!</div>
<div> </div>
<div>john<br> <br> </div>