-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathex2.6-church-numerals.scm
84 lines (60 loc) · 1.91 KB
/
ex2.6-church-numerals.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(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 (( (lambda (f) (lambda (x) x)) f) x))))
;->
(lambda (f) (lambda (x) (f ( (lambda (x) x) x))))
;->
(lambda (f) (lambda (x) (f x)))
; so ->
(define one (lambda (f) (lambda (x) (f x))))
(add-1 one)
;->
(lambda (f) (lambda (x) (f ( ( (lambda (f) (lambda (x) (f x))) f) x))))
;->
(lambda (f) (lambda (x) (f ( (lambda (x) (f x)) x) )))
;->
(lambda (f) (lambda (x) (f (f x) )))
; so ->
(define two (lambda (f) (lambda (x) (f (f x)))))
; so -> for body of add-1:
(lambda (x) (f ((n f) x) ) )
; ^
; this is actual add one |
; ^ ^
; this applies prameter | |
; +
(define (+ a b)
; (lambda (f) (lambda (x) x)))
; (lambda (f) (lambda (x) ((b f) x) )))
; (lambda (f) (lambda (x) ((a f) ((b f) x)) )))
(a b))
; for (define (+ a b) (a b))
; let's say + one and two
(a b)
; ->
( (lambda (f) (lambda (x) (f x))) (lambda (f) (lambda (x) (f (f x)))) )
; ->
(lambda (x) ( (lambda (f) (lambda (x) (f (f x)))) x))
; ->
(lambda (x) ( (lambda (x) (x (x x))) ) )
; which is wrong
; for (lambda (f) (lambda (x) ((a f) ((b f) x)) )))
; let's say + one and two
(lambda (f) (lambda (x) ((a f) ((b f) x)) ))
;->
(lambda (f) (lambda (x) (( (lambda (f) (lambda (x) (f x))) f) (( (lambda (f) (lambda (x) (f (f x)))) f) x)) ))
;->
(lambda (f) (lambda (x) ( ( (lambda (f) (lambda (x) (f x))) f) ( ( (lambda (f) (lambda (x) (f (f x)))) f) x)) ))
; ->
(lambda (f) (lambda (x) ( (lambda (x) (f x)) ( (lambda (x) (f (f x))) x)) ))
; ->
(lambda (f) (lambda (x) ( (lambda (x) (f x)) (f (f x)) ) ))
; ->
(lambda (f) (lambda (x) ( (f (f (f x)) ) )))
; so:
(define (+ a b)
(lambda (f) (lambda (x) ((a f) ((b f) x)) )))
;