nntpnews.net

Global Usenet Archiver


Register

[Haskell-beginners] defining expression

Reply

  #1  
Old 07-02-10, 09:14 PM
John Moore
 
Posts: n/a
Default [Haskell-beginners] defining expression

Hi,
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
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) = 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
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
[email]Beginners@haskell.org[/email]
[url]http://www.haskell.org/mailman/listinfo/beginners[/url]

Reply With Quote
  #2  
Old 07-02-10, 09:43 PM
Stephen Blackheath [to Haskell-Beginners]
 
Posts: n/a
Default Re: [Haskell-beginners] defining expression

John,

I'm seeing the error you describe "Couldn't match expected type `Expression'
against inferred type `IO b'" on this line:

evalStep d (Let n e1 e2) = do
putStrLn ("Adding definition for "++n) <---

The trouble here is that evalStep's type is :: Dict -> Expression ->
Expression, and Haskell doesn't allow general I/O in a function with a
return type other than IO <something>. This restrictiveness is pretty
much the whole point of Haskell. You have two choices, either...

1. Change the type of evalStep to Dict -> Expression -> IO Expression,
and of any function that calls it (and so on up the call chain), or

2. Use Debug.trace for your debug output, like this:

trace ("adding/removing definition of "++n) $
evalStep d (addEntry n e1 d) e2

If you want 'adding' and 'removing definition of' as separate messages
before and after (sequenced relative to other traces you might add),
this is not straightforward, but it can be done. Essentially in pure
Haskell code you have to think in terms of data dependency rather than
sequence in time.


Steve

John Moore wrote:
> Hi,
> 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 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) = 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
> 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
> [email]Beginners@haskell.org[/email]
> [url]http://www.haskell.org/mailman/listinfo/beginners[/url]

_______________________________________________
Beginners mailing list
[email]Beginners@haskell.org[/email]
[url]http://www.haskell.org/mailman/listinfo/beginners[/url]
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Haskell-beginners] defining functions in ghci Joe Van Dyk fa.haskell 9 19-02-10 09:24 PM
[Haskell-beginners] Problems defining a type with a multiplicationfunction Amy de Buitléir fa.haskell 4 09-09-09 11:50 PM
[Haskell-beginners] Urgent: Defining Momory data Types in Haskell Akshay Dave fa.haskell 3 08-09-09 01:52 PM
[Haskell-beginners] defining 'init' in terms of 'foldr' Michael Mossey fa.haskell 2 12-05-09 07:59 PM
[Haskell-beginners] defining own arbitrary :: Gen Int ben fa.haskell 1 07-03-09 02:26 PM


All times are GMT +1. The time now is 06:16 PM. Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0



For ads on this site use independent advertising companies. These companies may use some data (which does not include your name, address, email address or telephone number) about your visits to this and other websites to create advertisements on products and services you might enjoy. If you'd like more information and to know the options available to prevent the use of such information by these companies, click here