Blob


1 ;; Exercise 2.6. In case representing pairs as procedures wasn't mind-boggling enough, consider that, in a language that can manipulate procedures, we can get by without numbers (at least insofar as nonnegative integers are concerned) by implementing 0 and the operation of adding 1 as
3 (define zero (lambda (f) (lambda (x) x)))
5 (define (add-1 n)
6 (lambda (f) (lambda (x) (f ((n f) x)))))
8 ;; This representation is known as Church numerals, after its inventor, Alonzo Church, the logician who invented the calculus.
10 ;; Define one and two directly (not in terms of zero and add-1). (Hint: Use substitution to evaluate (add-1 zero)). Give a direct definition of the addition procedure + (not in terms of repeated application of add-1).
12 (define one (add-1 zero))
14 (lambda (f) (lambda (x) (f (((lambda (f) (lambda (x) x)) f) x))))
15 ;;(lambda (f) (lambda (x) (f ((n f) x))))
16 (lambda (f) (lambda (x) (f (((lambda (f) f)) x))))
17 (lambda (f) (lambda (x) (f ((lambda (f) f) x))))
18 (lambda (f) (lambda (x) (f x)))
19 (lambda (f) (lambda (x) (f x)))
21 (define one (lambda (f) (lambda (x) (f x))))
22 (define two (add-1 one))
24 (lambda (f) (lambda (x) (f (((lambda (f) (lambda (x) (f x))) f) x))))
25 (lambda (f) (lambda (x) (f ((lambda (x) (f x)) x))))
26 (lambda (f) (lambda (x) (f (f x))))
28 (define two (lambda (f) (lambda (x) (f (f x)))))
30 ;; Give a direct definition of the addition procedure + (not in terms of repeated application of add-1).
32 (define (+ a b)
33 (lambda (f) (lambda (x) ((a f) ((b f) x)))))