ML功能咖啡 [英] ML function currying

查看:160
本文介绍了ML功能咖啡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释一下这个概念吗?我主要是在学习它,因为我在我的'现代编程语言'类中学习了ML以用于函数式语言介绍。

特别是你可以使用这个例子:

  -fun ga = fn b => A + B; 
val g = fn:int - > int - > int
-g 2 3;
val it = 5:int

我很困惑这些参数是如何传递的甚至想到它。



感谢您的帮助。

解决方案

在这种情况下,你将currying明确地表达出来,所以应该更容易理解。



如果我们读取函数定义,它会说(释义):创建一个函数 g ,当给出 a 时返回 fn b => ; a + b



也就是说,如果我们调用 g 2 我们得到函数 fn b => 2 + B 。因此,当我们调用 g 2 3 时,我们实际上调用(g 2)3 ;那就是我们首先得到上面描述的函数,然后在 3 的值上使用这个函数,产生 5

Currying简单地说就是在几个阶段中创建一个函数的概念,每个阶段都接受一个输入并产生一个新函数。 SML为此提供了语法糖,使得 g 等价于以下内容:

  fun gab = a + b; 


Could someone please explain the concept of currying to me. I am primarily learning it because we are learning ML in my 'modern programming language' class for a functional language introduction.

In particular you can use this example:

    -fun g a = fn b => a+b;
      val g = fn: int -> int -> int
    -g 2 3;
      val it = 5 : int

I'm confused how these parameters are passed or how to even think about it in the first place.

Thank you for any help.

解决方案

In this case, you make the currying explicit, so it should be easier to understand.

If we read the function definition, it says (paraphrased): "Create a function g, which when given an a returns fn b => a+b."

That is, if we call g 2, we get back the function fn b => 2+b. As such, when we call g 2 3, we actually call (g 2) 3; that is we first get the function stated above back, and then use this function on the value 3, yielding 5.

Currying is simply the concept of making a function in several "stages", each taking an input and producing a new function. SML has syntactic sugar for this, making g equivalent to the following:

fun g a b = a + b;

这篇关于ML功能咖啡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆