[Haskell-beginners] defining expression

John Moore john.moore54 at gmail.com
Sun Feb 7 15:14:17 EST 2010

   This gives me the explaination that it expecting a Expression but it been
giving a IO b can some explain whats going on please.

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
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) = do
       putStrLn ("Adding definition for "++n)
       v <- evalStep d (addEntry n e1 d) e2
       putStrLn ("Removing definition for "++n)
       return v

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

       "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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100207/7a684da5/attachment-0001.html

More information about the Beginners mailing list