[Haskell-beginners] one more step backwards
John Moore
john.moore54 at gmail.com
Sun Jan 31 11:05:51 EST 2010
Hi ,
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.
data Expression = Val Integer
| Add Expression Expression
| Subtract Expression Expression
| Multiply Expression Expression
| Divide Expression Expression
deriving Show
demo1 = (Add(Add(Add(Add(Val 6)(Val 5))(Val 10))(Val 7))(Val 30))
evalStep :: Expression -> Expression
evalStep (Val x)= (Val x)
evalStep (Add x y)
= case x of
(Val a) -> case y of
(Val b) -> Val (a+b)
left -> Add x (evalStep y)
right -> Add (evalStep x)y
evaluate :: Expression -> IO ()
-- Base case
evaluate (Val a) = return ()
-- Recursive case
evaluate e = do
putStrLn "Evaluating one more step"
let e' = (evalStep e)
putStrLn ("Result is "++(show e'))
putStrLn "Do another step (y/n)? :"
c <- getLine
if (c=="y")then
evaluate e'
else putStrLn("Ok you said" ++ show[c] ++ "so that's it" ++ show
getCount)
getCount:: Expression -> Int
getCount e' = n, n=1
if(getCount == 1) then
putStrLn ("Cannot go back")
else putStrLn ("One step back" ++ show n-1)
All thoughts welcome!
john
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100131/bdde027c/attachment.html
More information about the Beginners
mailing list