1. ## What do you mean to do? x + 1 is not equal to x.  Reply With Quote

2. Junior Coders Join Date
Oct 2008
Posts
7
Thanks
5
Thanked 0 Times in 0 Posts

## 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  Reply With Quote

3. Senior Coders Join Date
May 2007
Location
USA
Posts
373
Thanks
2
Thanked 4 Times in 4 Posts

## 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.  Reply With Quote

4. ## 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 the `eval` function.

Note to you non-GHC folks:
Code:
```import Data.Function (on)
import Data.Maybe (fromMaybe)
import Data.List (sort, partition, intercalate)```
should be
Code:
```import Maybe (fromMaybe)
import List (sort, partition, intercalate)

(g `on` f) x y = f x `g` f y```  Reply With Quote

5. Junior Coders Join Date
Oct 2008
Posts
7
Thanks
5
Thanked 0 Times in 0 Posts

##  Originally Posted by Twey `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`

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.  Reply With Quote

6. Junior Coders Join Date
Nov 2008
Location
UK
Posts
9
Thanks
0
Thanked 0 Times in 0 Posts

## 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. Originally Posted by Twey eval :: [(Variable, Integer)][/icode] — 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`
Thanks m8 Last edited by Twey; 12-01-2008 at 06:15 AM.  Reply With Quote

7. ## `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:
Code:
```fromMaybe _ (Just x) = x
fromMaybe x Nothing  = x```
This is handy, because `lookup :: a -> [(a, b)] -> Maybe b` returns a `Maybe b`, with Nothing representing the case where no appropriate pair can be found; that is to say,
Code:
```lookup _ []            = Nothing
lookup x ((a, b) : xs)
| x == a           = Just b
| otherwise        = lookup x xs```
So, we pair these two up (`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.  Reply With Quote

8. ## 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.  Reply With Quote

9. Senior Coders Join Date
May 2007
Location
USA
Posts
373
Thanks
2
Thanked 4 Times in 4 Posts

## 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.  Reply With Quote

10. ## 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.  Reply With Quote

haskell 