lambdaschemefactoriallambda-calculus

Factorial function with just lambda expression


What is the most transparent and elegant factorial function you can create, on your own, using only lambda expressions?

One of my students took a Scheme class at Berkeley and was given this extra credit problem of creating the factorial function with only lambda expressions (no define, let, or other power procedures). It took me awhile to solve, and was complicated and ugly.

I am teaching Scheme now, a couple of years later, and I realized I was going to set it as a challenge for myself, and thought others might appreciate it as well.


Solution

  • The one I did a couple of years ago had twice as many lines and was much harder to follow.

    (lambda (n)
      ((lambda (fact) (fact fact 1 n))
       (lambda (f P n) (if (<= n 1) P (f f (* n P) (- n 1))))))