Add4使用Lambda表达式

时间:2014-08-14 18:06:47

标签: functional-programming lambda lambda-calculus

我知道使用lambda表达式,我们可以编写succ = λnfx • f (n f x )twice = λfn • f f(n )。我现在的目标是使用这两个来编写add4,它为教堂数字增加了4个。

如何为此编写beta减少步骤?

先谢谢你的帮助......

1 个答案:

答案 0 :(得分:3)

好吧,twice succ是一个应用succ两次的函数,因此twice (twice succ)是一个两次应用twice succ的函数,因此应用succ四次。所以它应该是add4 = twice (twice succ)

twice = λf.λn.(f (f n))
twice (twice succ)
=> twice (λf.λn.(f (f n)) succ)
=> twice λn.(succ (succ n))
=> λf.λn.(f (f n)) λn.(succ (succ n))
=> λf.λn.(f (f n)) λx.(succ (succ x))
=> λn.(λx.(succ (succ x)) (λx.(succ (succ x)) n))
=> λn.(λx.(succ (succ x)) (succ (succ n)))
=> λn.(succ (succ (succ (succ n))))
=> λn.(succ (succ (succ (succ n))))