需要帮助理解柯里化

发布于 2024-11-27 08:06:39 字数 531 浏览 0 评论 0原文

这是我从这本电子书中摘取的示例(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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏夜暖风 2024-12-04 08:06:39

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 as fn (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 calling f (x,y).

fun foo x y = ... is syntactic sugar for val foo = fn x => fn y => ..., so again foo 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 function foo with the argument 1 and then calls the resulting function with the argument 2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文