Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curried anonymous function in SML

Tags:

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?

like image 572
jjennifer Avatar asked Mar 13 '10 02:03

jjennifer


People also ask

What is currying in SML?

[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.

Which function is a small anonymous function?

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.

What is anonymous function with example?

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

What is the use of anonymous function?

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.


2 Answers

(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.

like image 107
sepp2k Avatar answered Sep 19 '22 17:09

sepp2k


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,

like image 36
Waverim Avatar answered Sep 19 '22 17:09

Waverim