3 ms·
This article uses the factorial function on Church numerals as motivation for discussing recursion and the Y-combinator. Yet Church numerals have enough recursi
by tromp 3y ago
This article uses the factorial function on Church numerals as motivation for discussing recursion and the Y-combinator.
Yet Church numerals have enough recursion "built-in" to compute factorials directly:
fac = λn.λf. n F (λx.f) (λx.x) where F = (λc.λn.n(c(λf.λx.n f(f x))))
For example, fac 3 evaluates as (writing Church numerals simply as 1,2...)
λf. F (F (F (λx.f))) 1
= λf. 1 (F (F (λx.f)) 2)
= λf. 1 (2 (F (λx.f) 3))
= λf. 1 (2 (3 ((λx.f) 4)))
= λf. 1 (2 (3 f))
= 6