I have the function below and it works:
(fn x => x * 2) 2;
but this one doesn't work:
(fn x y => x + y ) 2 3;
Can anyone tell me why? Or give me some hint to get it to work?
[Currying] Named after mathematician/logician Haskell Brooks Curry, currying is the act of transforming a function that takes multiple arguments into a function that returns a function that takes the remaining arguments.
A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression. The expression is evaluated and returned. Lambda functions can be used wherever function objects are required.
An anonymous function is a function that was declared without any named identifier to refer to it. As such, an anonymous function is usually not accessible after its initial creation. Normal function definition: function hello() { alert('Hello world'); } hello();
Anonymous functions, also known as closures , allow the creation of functions which have no specified name. They are most useful as the value of callable parameters, but they have many other uses. Anonymous functions are implemented using the Closure class.
(fn x => fn y => x+y) 2 3;
works. fn
simply doesn't have the same syntactic sugar to define curried functions that fun
has.
In Standard ML, a function can have only one argument, so use
(fn (x,y) => x + y) (2,3)
and the type is
fn: int * int -> int
in this time (x,y) and (2,3) is a list structure,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With