需要帮助理解柯里化
这是我从这本电子书中摘取的示例(http://www.cs.cornell.edu/riccardo/prog-smlnj/notes-011001.pdf)
-fun curry (f:'a * 'b -> 'c) = fn (x:'a) => fn (y:'b) => f (x,y);
val curry = fn : ('a * 'b -> 'c) -> 'a -> 'b -> 'c
如何解释此函数。 Curry 将 'a * 'b -> 类型的函数 f 作为参数。 'c。我无法理解“=”后面的部分。什么是关联顺序?
这是另一个例子:
fun add’ (x:int) (y:int):int = x + y;
这是如何解析的?
维基百科说“柯里化是一种对采用多个参数(或参数的 n 元组)的函数进行转换的技术,使其可以被称为函数链,每个函数都具有单个参数(部分应用)”。哪个是单个参数:多个参数中的第一个还是最后一个?
Here is an example I picked up from this ebook (http://www.cs.cornell.edu/riccardo/prog-smlnj/notes-011001.pdf)
-fun curry (f:'a * 'b -> 'c) = fn (x:'a) => fn (y:'b) => f (x,y);
val curry = fn : ('a * 'b -> 'c) -> 'a -> 'b -> 'c
How do I interpret this function. Curry takes as argument a function f of type 'a * 'b -> 'c. I can't understand the part after '='. What is the associativity order?
Here is another example:
fun add’ (x:int) (y:int):int = x + y;
How is this parsed?
Wikipedia says "currying is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application)". Which is the single argument: the first or the last one out of the multiple arguments?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
fn (x:'a) =>; fn(y:'b)=> f (x,y)
被解析为fn (x:'a) => (fn (y:'b) => f (x,y))
。因此,您有一个函数,它接受 a 类型的参数 x 并返回另一个函数,该函数接受 b 类型的参数 y。然后,该另一个函数返回调用f (x,y)
的结果。fun foo xy = ...
是val foo = fn x =>; 的语法糖。 fn y => ...
,所以foo
又是一个函数,它接受一个参数 x 并返回另一个函数,该函数接受一个参数 y。类似地,调用
foo 1 2
将被解析为(foo 1) 2
,即它使用参数1调用函数foo
,然后调用带有参数 2 的结果函数。fn (x:'a) => fn (y:'b) => f (x,y)
is parsed asfn (x:'a) => (fn (y:'b) => f (x,y))
. So you have a function, which takes an argument x of type a and returns another function, which takes an argument y of type b. This other function then returns the result of callingf (x,y)
.fun foo x y = ...
is syntactic sugar forval foo = fn x => fn y => ...
, so againfoo
is a function, which takes one argument x and returns another function, which takes one argument y.Similarly the call
foo 1 2
will be parsed as(foo 1) 2
, i.e. it calls the functionfoo
with the argument 1 and then calls the resulting function with the argument 2.