Showing posts with label Y combinator. Show all posts
Showing posts with label Y combinator. Show all posts
Friday, September 27, 2019
Can you write factorial anonymously? Yes
Of course you can. Use the Y-combinator.
This would look cleaner if the author had not used Church numerals. Here's something simpler:
(λf λn. if n=0 then 1 else n*f(n-1) ) (λf λn. if n=0 then 1 else n*f(n-1)) 6.
But this isn't quite right, since recursion stops after one step. The Y combinator is more devious even than this.
So the factorial function is: Y (λf λn. if n=0 then 1 else n*f(n-1) ).
See section 7 of this PDF for a worked example.
---
Subscribe to:
Posts (Atom)