
Hi All, Trying to get this to work, keeps telling me there a parse error on right in the let expression. Can anyone see where the problem is and be able to explain it to me. import Maybe data Expression = Val Double | Add Expression Expression | Subtract Expression Expression | Multiply Expression Expression | Divide Expression Expression | Var String | Let String Expression Expression deriving Show demo1 = (Add(Multiply(Divide(Subtract(Val 25)(Val 5))(Val 10))(Val 7))(Val 30)) type Dict =[(String,Expression)] emptyDict :: Dict emptyDict = [] addEntry :: String->Expression ->Dict -> Dict addEntry n e d = (n,e): d lookupEntry :: String -> Dict -> Maybe Expression lookupEntry n [] = Nothing lookupEntry n (x:xs) = if (n == k) then (Just v) else lookupEntry n xs where (k,v) = x evalStep :: Dict -> Expression -> Expression evalStep d(Val x)= (Val x) evalStep d(Add x y) = case x of (Val a) -> case y of (Val b) -> Val (a+b) left -> Add x (evalStep d y) right -> Add (evalStep d x)y evalStep d(Subtract x y) = case x of (Val a) -> case y of (Val b) -> Val (a-b) left -> Subtract x (evalStep d y) right -> Subtract (evalStep d x)y evalStep d(Multiply x y) = case x of (Val a) -> case y of (Val b) -> Val (a*b) left -> Multiply x (evalStep d y) right -> Multiply (evalStep d x)y evalStep d (Divide x y) = case x of (Val a) -> case y of (Val b) -> Val (a/b) left -> Divide x (evalStep d y) right -> Divide (evalStep d x)y evalStep d (Let n e1 e2) = case e1 of (Val a) -> case e2 of (Val b)-> Val (Let e1 e2) left -> Let e1 (evalStep d e2) right -> Let (evalStep d e1) e2 evaluate :: Dict-> [Expression] -> Expression -> IO() evaluate d(x:xs) e = do putStrLn (show e) putStrLn "Do another step (y/n) or rollback (r)? :" c <- getLine case c of "y" -> let e'= (evalStep d e)in evaluate d (e:x:xs) e'-- build up history "r" -> case (x:xs) of (x:xs)-> evaluate d xs x []-> do { putStrLn "Empty" ;evaluate d(x:xs) e } "n" -> putStrLn $ "Ok you said no :" ++ c John

Hi John
Provided the whitespace doesn't get mangled in the mail the last
evalStep needs indenting correctly:
evalStep d (Let n e1 e2)
= case e1 of
(Val a) -> case e2 of
(Val b)-> Val (Let e1 e2)
left -> Let e1 (evalStep d e2)
right -> Let (evalStep d e1) e2
Also the "r" case in evaluate:
"r" -> case (x:xs) of
(x:xs) -> evaluate d xs x
[] -> do { putStrLn "Empty"
;evaluate d(x:xs) e
}
You might want to consider using wildcards (i.e. underscores) _ rather
than variables e.g 'left' and 'right' when you don't use the value of
a pattern match at the right of the "->".
Best wishes
Stephen
On 10 February 2010 20:16, John Moore
Hi All, Trying to get this to work, keeps telling me there a parse error on right in the let expression. Can anyone see where the problem is and be able to explain it to me.
import Maybe data Expression = Val Double | Add Expression Expression | Subtract Expression Expression | Multiply Expression Expression | Divide Expression Expression | Var String | Let String Expression Expression deriving Show demo1 = (Add(Multiply(Divide(Subtract(Val 25)(Val 5))(Val 10))(Val 7))(Val 30)) type Dict =[(String,Expression)] emptyDict :: Dict emptyDict = [] addEntry :: String->Expression ->Dict -> Dict addEntry n e d = (n,e): d lookupEntry :: String -> Dict -> Maybe Expression lookupEntry n [] = Nothing lookupEntry n (x:xs) = if (n == k) then (Just v) else lookupEntry n xs where (k,v) = x evalStep :: Dict -> Expression -> Expression evalStep d(Val x)= (Val x) evalStep d(Add x y) = case x of (Val a) -> case y of (Val b) -> Val (a+b) left -> Add x (evalStep d y) right -> Add (evalStep d x)y evalStep d(Subtract x y) = case x of (Val a) -> case y of (Val b) -> Val (a-b) left -> Subtract x (evalStep d y) right -> Subtract (evalStep d x)y evalStep d(Multiply x y) = case x of (Val a) -> case y of (Val b) -> Val (a*b) left -> Multiply x (evalStep d y) right -> Multiply (evalStep d x)y evalStep d (Divide x y) = case x of (Val a) -> case y of (Val b) -> Val (a/b) left -> Divide x (evalStep d y) right -> Divide (evalStep d x)y evalStep d (Let n e1 e2) = case e1 of (Val a) -> case e2 of (Val b)-> Val (Let e1 e2) left -> Let e1 (evalStep d e2) right -> Let (evalStep d e1) e2
evaluate :: Dict-> [Expression] -> Expression -> IO() evaluate d(x:xs) e = do putStrLn (show e) putStrLn "Do another step (y/n) or rollback (r)? :" c <- getLine case c of "y" -> let e'= (evalStep d e)in evaluate d (e:x:xs) e'-- build up history
"r" -> case (x:xs) of (x:xs)-> evaluate d xs x []-> do { putStrLn "Empty" ;evaluate d(x:xs) e } "n" -> putStrLn $ "Ok you said no :" ++ c
John _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Am Mittwoch 10 Februar 2010 21:16:31 schrieb John Moore:
Hi All, Trying to get this to work, keeps telling me there a parse error on right in the let expression. Can anyone see where the problem is and be able to explain it to me.
evalStep d (Let n e1 e2) = case e1 of (Val a) -> case e2 of (Val b)-> Val (Let e1 e2) left -> Let e1 (evalStep d e2) right -> Let (evalStep d e1) e2
Here? Indentation. The patterns for the inner case-expression must be indented further than those for the outer (and all patterns in one case-expression must be indented to the same level).
evaluate :: Dict-> [Expression] -> Expression -> IO() evaluate d(x:xs) e = do putStrLn (show e) putStrLn "Do another step (y/n) or rollback (r)? :" c <- getLine case c of "y" -> let e'= (evalStep d e)in evaluate d (e:x:xs) e'-- build up history
"r" -> case (x:xs) of (x:xs)-> evaluate d xs x []-> do { putStrLn "Empty" ;evaluate d(x:xs) e } "n" -> putStrLn $ "Ok you said no :" ++ c
John
participants (3)
-
Daniel Fischer
-
John Moore
-
Stephen Tetley