What do you mean to do? x + 1 is not equal to x.
Printable View
What do you mean to do? x + 1 is not equal to x.
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"
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 theevalfunction.
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
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"
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 (:confused: !!!), 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 :cool:
fromMaybe :: a -> Maybe a -> atakes 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 = x
lookup :: a -> [(a, b)] -> Maybe breturns 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 xs
eval 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.
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. :)
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.
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 :p It's disturbingly rare.