; A "true factorial function" written using PAIR
(define FACT-helper
(lambda (p)
(PAIR (INC (LEFT p))
(MUL (INC (LEFT p)) (RIGHT p))) ) )
(define FACT
(lambda (n)
(RIGHT ((n FACT-helper) (PAIR ZERO ONE))) ) )
; The functional for the factorial function.
; Note that the "true factorial function" (FACT) is
; fixpoint of FACT-functional.
(define FACT-functional
(lambda (fact)
(lambda (n)
(IFX (ZEROP n) ; IFX is lambda-calc version of IF
ONE
(MUL n (fact (DEC n))) ) ) ) )
; Try evaluating
; ]=> (print-church ( (FACT-functional FACT) FOUR ) )
; Detour: If we realize that we need only n, not an infinite number of
; cascaded applications of fact-functional, we can cheat by exploiting
; the cascading power of Church numerals.
(define FACT-church
(lambda (n)
( ( (lambda (m) ((n m) ZERO ) ) FACT-functional ) n ) ) )
; But how about general recursion?