1

I am using ghci compiler with version 7.8.3 on windows 7. I am getting error message showing parse error on input `->'. I have the following code for lambda expression in haskell.

add =\x y -> x+y

1
  • Note that =\ is an operator. Since you want to assign a lambda expression to the name add, you need a space: = \ . Commented Nov 1, 2014 at 18:51

2 Answers 2

5

When defining a function interactively in ghci, you have to bind it using a let like this:

let add = \x y -> x + y
Sign up to request clarification or add additional context in comments.

2 Comments

Not if it's globally stated function. If that is the case though, it should just be written in sugared form add x y = x + y, or just add = (+).
Also, the = and \ must be separated by space like here, because =\ is a legal operator name.
2

I just wrote:

add = \x y -> x + y

main = do
    print $ add 1 2

and it compiled and output 3.

Unless your intent is to practice unsugared code though, I would write it out as:

add x y = x + y

Or

add = (+)

which is "point-free" form.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.