简体   繁体   中英

Haskell to Javascript Lamba function translation

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)

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); }

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM