1. Which university is that (if you don't mind my asking)? Lamentably few have functional programming on the curriculum.

2. Junior Coders
Join Date
Nov 2008
Location
UK
Posts
9
Thanks
0
Thanked 0 Times in 0 Posts
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.

3. 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.

4. Junior Coders
Join Date
Nov 2008
Location
UK
Posts
9
Thanks
0
Thanked 0 Times in 0 Posts
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

5. Well, one at a time, then:
1. `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 request`
2. `toString :: 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, and `toString'`, which performs everything else.
Code:
`-- code removed by request`
3. `eval :: [(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 from `Maybe Integer` to `Integer` 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`
4. For extra credit, you may also want to implement a Num instance for Expr, allowing you to write `5 * 3 + 2` instead of `Mult (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.

6. Senior Coders
Join Date
May 2007
Location
USA
Posts
373
Thanks
2
Thanked 4 Times in 4 Posts
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.

7. Careful — that's evaluation you're doing in `simplify` (likewise in `negate`).

8. Junior Coders
Join Date
Nov 2008
Location
UK
Posts
9
Thanks
0
Thanked 0 Times in 0 Posts
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".

9. Similar, but it matches any Expr, not just a Var "A".

10. Junior Coders
Join Date
Nov 2008
Location
UK
Posts
9
Thanks
0
Thanked 0 Times in 0 Posts
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