2

I have the following lambda function in haskell:

cup size = \message -> message size

I would like to know what is the equivalent version in JavaScript (for learning purpose), currently I wrote the following version, I would like to if it is correct.

const cup = size => (message => message)(size)
2
  • Is message a method ? Because if yes i do not get the usage of lambda.You could write cup=message and in js cup=str=>message(str) Commented Feb 10, 2019 at 21:03
  • 4
    @BercoviciAdrian cup is not message; cup is a function that returns a function which applies its argument to cup's argument. One could also write cup size message = message size, which makes it more apparent that cup = flip ($). Commented Feb 10, 2019 at 21:40

2 Answers 2

8

Your JavaScript code corresponds to

cup = \size -> (\message -> message) size

in Haskell. Because \message -> message is the identity function, this is the same as

cup = \size -> size

which is the identity function again:

cup = id

The correct translation would be

const cup = size => message => message(size)

or

function cup(size) { return message => message(size); }
Sign up to request clarification or add additional context in comments.

Comments

3

Your haskell lambda takes an argument and returns a lambda which in turn takes a function as argument and applies that function with the argument given to cup.

In javascript, the equivalent would be this:

const cup = size => (message => message(size))

You can rewrite it without the parenthesis:

const cup = size => message => message(size)

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.