-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample.plsh
116 lines (77 loc) · 1.9 KB
/
sample.plsh
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
; conscell operations
(car (cdr '(a b c)))
; currying by lexical scope
((((lambda (x)
(lambda (y)
(lambda (z)
(cons x (cons y (cons z nil))))))
'a)
'b)
'c)
; simple Map function
(def map1
(lambda (f a)
(cond ((eq a nil) nil)
(t (cons (f (car a))
(map1 f (cdr a)))))))
; association list
(def al '((Apple . 120) (Orange . 210) (Lemon . 180)))
; higher-order function
(map1 car al)
(map1 cdr al)
; atom variable of lambda
(def list (lambda x x))
(def cadr (lambda (x) (car (cdr x))))
; definition of let by using macro
(def let
(macro (vvs body)
(cons (list 'lambda (map1 car vvs) body)
(map1 cadr vvs))))
(let ((x 'a) (y 'b)) (cons x y))
; search association list
(def assoc
(lambda (k vs)
(cond ((eq vs nil) nil)
((eq (car (car vs)) k)
(car vs))
(t (assoc k (cdr vs))))))
(assoc 'Orange al)
; implementation of Common Lisp sublis
(def sublis
(lambda (al L)
(cond ((eq L nil) nil)
(t (cons (let ((r (assoc (car L) al)))
(cond ((eq r nil) (car L))
(t (cdr r))))
(sublis al (cdr L)))))))
(def sl '((x . I) (y . you)))
(sublis sl '(x and y))
(def revapp
(lambda (x y)
(cond ((eq x nil) y)
(t (revapp
(cdr x)
(cons (car x) y))))))
(def reverse (lambda (x) (revapp x nil)))
(reverse '(a b c d e))
(def append
(lambda (x y)
(revapp (revapp x nil) y)))
(append '(a b c) '(x y z))
(def + append)
(def 1- (lambda (x) (cdr x)))
; lists as numbers
(def 0 nil)
(def 1 '(p))
(def 5 '(p p p p p))
(def N (+ 5 5))
(length N)
; Fibonacci number by fix-point combinator
(length
(((lambda (g) (g g))
(lambda (g)
(lambda (n a b)
(cond ((eq n 0) a)
(t ((g g) (cdr n)
b (append a b)))))))
N 0 1))