-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.06.scm
43 lines (34 loc) · 1.19 KB
/
2.06.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
;; Church numerals
(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 (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 (y) (f y)) x)))))
;; (lambda (f) (lambda (x) (f (f x))))
(define two
(lambda (f) (lambda (x) (f (f x)))))
;; inspired by add-1
(define (+ a b)
(lambda (f) (lambda (x) ((b f) ((a f) x)))))
;; test case
(+ zero one)
;; (lambda (f) (lambda (x) ((one f) ((zero f) x))))
;; (lambda (f) (lambda (x) ((one f) ((lambda (y) y) x))))
;; (lambda (f) (lambda (x) ((one f) x)))
;; (lambda (f) (lambda (x) ((lambda (y) (f y)) x)))
;; (lambda (f) (lambda (x) (f x)))
(+ one two)
;; (lambda (f) (lambda (x) ((two f) ((one f) x))))
;; (lambda (f) (lambda (x) ((two f) ((lambda (y) (f y)) x))))
;; (lambda (f) (lambda (x) ((two f) (f x))))
;; (lambda (f) (lambda (x) ((lambda (y) (f (f y))) (f x))))
;; (lambda (f) (lambda (x) ((lambda (y) (f (f y))) (f x))))
;; (lambda (f) (lambda (x) (f (f (f x)))))