Would this be the correct way of writing Mult function with Lambda

I wish these books gave us some sort of answers.
I am following this book.

It asks:
–Show how a the curried function definition mult x y z = x * y * z can be understood in terms of lambda expressions.

Here is what I came up with. is this correct ?

mult = λx -> (λy -> (λz -> x * y * z)

This solution is goin based of another example someone mentioned yesterday

where add’ x y could be written as

add' x  = /y = x + y
1 Like

It is correct. Remember that, as λ extends as far to the right as possible, brackets are redundant.

The second edition of Hutton’s book does have solutions to many of the problems. However, you don’t really need solutions, since you have GHCi to check everything. If GHCi is giving you an error, which it will with both of the examples you’ve written, then you know they are not correct.

Personally, I could never get unicode to work correctly with my GHCi so I am just going to use \ for the lambda symbol.

mult = \x -> (\y -> (\z -> x * y * z)

is almsot correct not not quite, because it has a syntax error: you are missing a second closing parenthesis. It should be

mult = \x -> (\y -> (\z -> x * y * z))

and as f-a pointed out, the parentheses are redundant, it could be written

mult = \x -> \y -> \z -> x * y * z

Similarly, your second example also has some syntax errors, namely you used / instead of \ for a lambda, and you cannot have a second equals sign in the middle of a function declaration. You probably meant to put an arrow instead of the second one. It should look like this

add' x = \y -> x + y

You should check everything that you’re writing in GHCi, if it gives you an error, that means something about it is incorrect. In this case you had the main ideas right but you had a few syntax errors preventing the code from being accepted.

1 Like

Thank you for the help. I will check my work in GHCi from now on.

1 Like