Curried anonymous function in SML

前端 未结 3 625
Happy的楠姐
Happy的楠姐 2021-02-07 02:55

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;
         


        
3条回答
  •  我寻月下人不归
    2021-02-07 03:38

    The answers posted above are correct. SML functions take only one argument. As a result, SML functions can have only one of two input types :

    1) t = (t1 * t2 * ... * tN) , for some N

    2) t = a, for some a.

    So, technically speaking, SML only takes product types or unary types as arguments to functions. One can more generally think of this as an Unary-Type or a projection of some product Type.

    In order to have currying inside anonymous functions, feel free to nest them inside each other as : fn x1 => fn x2 => ... fn xN => ...

    I think it's also important to know that : fun a = fn x1 => fn x2 => ... fn xN => ... is the full expansion of the syntactic sugar : fun a x1 x2 .. xN

提交回复
热议问题