Combinator组合子———— 模拟递归(一)

来源:互联网 发布:阿里云备案要邮寄吗 编辑:程序博客网 时间:2024/06/15 20:48

一系列概念:
“A combinator is just a lambda expression with no free variables.”
组合子是不包含自由变量的表达式。
“It’s a variable (i.e. a name or identifier in the language) which isn’t a bound variable. ”
自由变量不是一个绑定变量。
“A bound variable is simply a variable which is contained inside the body of a lambda expression that has that variable name as one of its arguments.”
绑定变量是lambda函数体中的变量,这个变量是lambda参数中的一个变量。

另外一点需要注意的是:+,- 或者其他任何的一个函数都可能成为自由变量。

利用组合子我们可以模拟一个递归:

def almostA = {//函数体是一个组合子————它不包含自由变量  fx: (Int=>Int) => {    n: Int => {      if (n == 0)        1      else        fx(n - 1) * n    }  }}def id = { x: Int => x }def A (n: Int): Int = {  almostA(id)(n)}

A函数不是递归,它没有调用自身如果你把这段代码放在idea IDE中,它并不会给出递归的符号。
你可以像使用一个递归一样使用A——除了,调用A(1),下一节会介绍完整的实现。

参考:http://mvanier.livejournal.com/2897.html

0 0