Which university is that (if you don't mind my asking)? Lamentably few have functional programming on the curriculum.
Which university is that (if you don't mind my asking)? Lamentably few have functional programming on the curriculum.
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!
Its called City University in London, sounds like scarface might go to the same one as me lol. What was the Russian asking Twey? I find Haskell fun once I know what am I am doing, although I dont find it easy to do.
They had the toString part of the exercise done, but wanted help refactoring it (theirs was a huuuuge twenty-line monster, dealing with very specific cases where simpler cases and recursion could have been used instead).
It gets easier as you go.
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!
Oh crap really???
To be honest I am completely stuck , I understand that I need to simplify expressions etc, just not to sure how to go about it. I have posted the question on hpaste, re-read all my lecture notes, read the course text and asked in the #haskell channel too.
I keep getting told to bog off when really all I am after is for someone to help explain what needs to be done and maybe give me an example, myself and others found that the exercise is not totally clear. I’m not into copying, I would rather learn it myself so I fully understand it.
Here is the full exercise: http://hpaste.org/12333
Any kind of help is appreciated
Well, one at a time, then:
simplify :: Expr -> Expr
— this one is pretty simple. There's not much in the way of cleverness you can do here: simply specify each case where a simplification is possible, and then give a catch-all case for everything else. Example:Code:-- code removed by requesttoString :: Expr -> Expr
— not too tricky — half the exercise is done for you already. The only thing to watch out for is making sure that no brackets appear at the top level. To this end, we'll actually define two functions:toString
, which performs the conversion of complex operations without brackets, andtoString'
, which performs everything else.Code:-- code removed by requesteval :: [(Variable, Integer)]
— this one's a little bit trickier, since it involves looking something up as well. Luckily, there's a built-in function for this:lookup :: (Eq a) => a -> [(a, b)] -> Maybe b
. There's even a built-in to handle the conversion fromMaybe Integer
toInteger
for us: it's called fromMaybe, and it's in the package Maybe, whence we will import it (beware: imports must go at the top of the module, just underneath the module declaration). Apart from the Var case, this is all straightforward too:Code:-- code removed by request- For extra credit, you may also want to implement a Num instance for Expr, allowing you to write
5 * 3 + 2
instead ofMult (Const 5) (Plus (Const 3) (Const 2))
:Code:-- this code is not part of the exercise, and can therefore stay. instance Num Expr where (+) = Plus (*) = Mult (-) = Minus negate = Mult (-1) fromInteger = Const -- We can't implement these two without some more operations. abs = error "abs not defined on Exprs" signum = error "signum not defined on Exprs"
Last edited by Twey; 12-01-2008 at 06:19 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!
Heh, forgot about the Num class. I'm too tired to make simplify be 'good', so I have some filler in it for the time being. Here's what I did:
Code:-- code removed by request
Last edited by Twey; 12-01-2008 at 06:18 AM.
Trinithis
Careful — that's evaluation you're doing insimplify
(likewise innegate
).
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!
Hey Twey you have a been a big help, thanks for explaining all that to me, makes a bit more sense now.
Just wondered what this does:
simplify (Plus x (Const 0)) = simplify x works
As I have tried:
simplify (Plus (Var "A") (Const 0)) = Var "A"
The above works for me, however just wondered how yours worked and if it returns something similar to Var "A".
Similar, but it matches any Expr, not just a Var "A".
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!
Ah ok, I currently have:
simplify (Plus (Var a) (Const 0)) = Var a
simplify (Plus (Var a) (Const 1)) = Var a
simplify (Plus (Const 0) (Var b)) = Var b
simplify (Plus (Const 1) (Var b)) = Var b
However, this works for just 0 and 1, but if I get 2, 5, 100 etc its useless. Is there some way of making a simplifly case that does it all for plus? (Using plus for now instead of minus and mult).
Thanks
Bookmarks