Solution to Exercise SICP 2.6

Structure and Interpretation of Computer Programs

One solution to Exercise 2.6:

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

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

; (add-1 zero)
; (lambda (f) (lambda (x) (f ((zero f) x))))
; (lambda (f) (lambda (x) (f (((lambda (g) (lambda (y) y)) f) x))))
; (lambda (f) (lambda (x) (f ((lambda (y) y) x))))
; (lambda (f) (lambda (x) (f x)))
(define one
(lambda (f) (lambda (x) (f x))))

; (add-1 one)
;(lambda (f) (lambda (x) (f ((one f) x))))
;(lambda (f) (lambda (x) (f (((lambda (g) (lambda (y) (g (y)))) f) x))))
;(lambda (f) (lambda (x) (f ((lambda (y) (f (y))) x))))
;(lambda (f) (lambda (x) (f (f (x)))))
(define two
(lambda (f) (lambda (x) (f (f x)))))

(define (add a b)
(lambda (f) (lambda (x) ((a f) ((b f) x)))))

; transform Church numerals to integers (for testing)
(define (to-integer n)
(define (inc x) (+ 1 x))
((n inc) 0))

Advertisements