What do you mean to do? x + 1 is not equal to x.
What do you mean to do? x + 1 is not equal to x.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Use precedence and associativity to reduce the number of parentheses in the string produced by toString, e.g.
toString (Mult (Plus (Var "x") (Const 1)) (Var "y"))
= "(x + 1) * y"
toString (Plus (Plus (Var "x") (Const 1)) (Var "y"))
= "x + 1 + y"
toString (Plus (Var "x") (Plus (Const 1) (Var "y")))
= "x + (1 + y)"
toString (Plus (Var "x") (Mult (Const 1) (Var "y")))
= "x + 1 * y"
i am struggling with this? the code provided does not simplify all the above? please help thanks
I jazzed up my simplification code. Perhaps it's just me (as I've never done it before), but I highly suspect that there is a much better way of coding simplifications.
Code:-- code removed by request
Examples
Code:*Expr> let [x, y, z] = map (Var . return) "xyz" *Expr> *Expr> let test = toString . simplify *Expr> *Expr> test $ -(-x) + x + (99 + y) * x "(2x + (x * (99 + y)))" *Expr> *Expr> test $ -1 - x + 22 * x * (y + y + y) * (2 * z) + 9 "(8 - x + (132 * x * y * z))" *Expr> *Expr> test $ x * y * 9 * x * 0 * x "0" *Expr> *Expr> test $ x - x "0"
Last edited by Twey; 12-01-2008 at 06:17 AM. Reason: Remove code — sorry Trin, I saved it if you want it back.
Trinithis
Ahh, lists! I should have thought of that.
I'm still uncertain whether doing actual calculation is against the rules or not. I suspect it might be, but then perhaps 'evaluation' only means the variable lookup and interpretation. That does seem to be the focus of theeval
function.
Note to you non-GHC folks:should beCode:import Data.Function (on) import Data.Maybe (fromMaybe) import Data.List (sort, partition, intercalate)Code:import Maybe (fromMaybe) import List (sort, partition, intercalate) (g `on` f) x y = f x `g` f y
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
could you add to the above so when i put the below expressions in i get the results shown?
need to keep this simple. thanks
Use precedence and associativity to reduce the number of parentheses in the string produced by toString, e.g.
toString (Mult (Plus (Var "x") (Const 1)) (Var "y"))
= "(x + 1) * y"
toString (Plus (Plus (Var "x") (Const 1)) (Var "y"))
= "x + 1 + y"
toString (Plus (Var "x") (Plus (Const 1) (Var "y")))
= "x + (1 + y)"
toString (Plus (Var "x") (Mult (Const 1) (Var "y")))
= "x + 1 * y"
Last edited by Twey; 12-01-2008 at 06:15 AM.
Sorry for not replying, been pretty hectic this week. Ignore my last post too, got confused on something.
Ok I think I am done for my simplify and toString operations ( !!!), and I am going to leave them for the time being.
I am now working on my eval operation, however in your example Twey, what does the env do? Was slightly confused on that.
Thanks m8
Last edited by Twey; 12-01-2008 at 06:15 AM.
fromMaybe :: a -> Maybe a -> a
takes two parameters, a value to provide in the Nothing case and a Maybe, and returns either the value of the Just or the provided value. That is to say:This is handy, becauseCode:fromMaybe _ (Just x) = x fromMaybe x Nothing = xlookup :: a -> [(a, b)] -> Maybe b
returns aMaybe b
, with Nothing representing the case where no appropriate pair can be found; that is to say,So, we pair these two up (Code:lookup _ [] = Nothing lookup x ((a, b) : xs) | x == a = Just b | otherwise = lookup x xseval env (Var x) = fromMaybe 0 (lookup x env)
) and get a function that looks up the variable in the environment and, if it doesn't exist, returns 0.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Nicely found. Yes, I will remove the complete code (but leave the suggestions and explanations).
I wonder if you would enlighten me as to the original source of this exercise? I must say, I've amassed a fair amount of curiosity about it by now.
Last edited by Twey; 12-01-2008 at 06:24 AM.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
If only my school (Berkeley) taught functional programming with Haskell instead of Scheme, I would be soo happy.
As a side note, I think I have a better idea for simplification. Instead of transforming to a list, transform it to a graph, where edges represent either addition or multiplication between entities. I'm going to try it out when I have time.
Trinithis
Well, Scheme's simple syntax does make a lot of sense for a teaching language. There's much less to get in the way. Think yourself lucky you get taught functional programming at all It's disturbingly rare.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Bookmarks