3
votes

Assume we have a programming language that doesn’t support numbers or booleans: a lambda is the only value it provides.

Church numerals use lambdas to create a representation of numbers. The idea is closely related to the functional representation of natural numbers, i.e. to have a natural number representing “zero” and a function “succ” that returns the successor of the natural number it was given.

(define zero (lambda (f) (lambda (x) x)))

(define (add-1 n)
  (lambda (f) (lambda (x) (f ((n f) x)))))

If I have another lambda

(define pluss4 (lambda(x) (+ x 4))

I can test it like: (pluss4 0) and I see that answer is 4

How to test Church numerals and see some result?

1

1 Answers

0
votes

To transform a church number to an integer you just apply it like this:

(define (add1 n) (+ n 1))         ; In SICP it's called 1+
((zero add1) 0)                   ; ==> 0
(((add-1 (add-1 zero)) add1) 0)   ; ==> 2

You can make it a procedure:

(define (church->int x)
  ((x add1) 0))

(church->int zero)                  ; ==> 0
(church->int (add-1 (add-1 zero)))  ; ==> 2