Числа Чёрча на Scheme

 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
; church_numerals.sch
; Definitions
(define _0
(lambda (f)
(lambda (x)
x)))
(define (_1+ n)
(lambda (f)
(lambda (x)
(f ((n f) x)))))
(define (_1- n)
(lambda (f)
(lambda (x)
(((n (lambda (g)
(lambda (h)
(h (g f)))))
(lambda (u) x))
(lambda (u) u)))))
(define (_+ n m) ((m _1+) n))
(define (_- n m) ((m _1-) n))
(define (_* n m)
(lambda (f)
(lambda (x)
((n (m f)) x))))
(define (_^ n m) (m n))
; Testing
(define _1 (_1+ _0))
(define _2 (_1+ _1))
(define _3 (_1+ _2))
(define _4 (_^ _2 _2))
(define _6 (_* _2 _3))
(define _5 (_1- _6))
(define (_ n)
((n (lambda (x) (+ x 1))) 0))
(_ (_+ _4 _3)) ; 7
(_ (_- _6 _2)) ; 4
(_ (_* _6 _5)) ; 30
(_ (_^ _2 _6)) ; 64