-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTESTFUNS.LSP
112 lines (83 loc) · 3.43 KB
/
TESTFUNS.LSP
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
(setq alli
'((a l l i) (p a m a u t t i) (v i t t u a) (m a p i l l a)))
(setq sika
'((i s o) (r i k a s) (s i k a) (s | k |) (s a k i s s a) (k i r o s i)))
(setq isi '((I S I) (A H !) (E L L I) (K A N I) (N A K I L L E) (H A I S I)))
(defun soy (sauce)
(soy (add1 (print sauce))))
(defun ! (x)
(cond ((zerop x) 1)
((* x (! (sub1 x))))))
(defun fib (n) /* From LISP, page 65 */
(cond ((zerop n) 1)
((eq n 1) 1)
(t (+ (fib (sub1 n))
(fib (- n 2))))))
(defun count-atoms (l) ; From LISP, page 69
(cond ((null l) 0)
((atom l) 1)
((+ (count-atoms (car l))
(count-atoms (cdr l))))))
(defun mystery (s) ; From LISP, page 71, counts maximum depth of list
(cond ((null s) 1)
((atom s) 0)
(t (max (+ (mystery (car s)) 1)
(mystery (cdr s))))))
(defun strange (l) ; From LISP, page 71, copies a list (at every level)
(cond ((null l) nil)
((atom l) l)
(t (cons (strange (car l))
(strange (cdr l))))))
/* Problem 4-3 from LISP, page 71, solution at page 342 */
(defun tail-reverse (lista) (tail-reverse-aux lista ()))
(defun tail-reverse-aux (lista rev)
(cond ((null lista) rev)
(t (tail-reverse-aux (cdr lista) (cons (car lista) rev)))))
/* Rekursiivisia funktioita Hyvosen & Seppasen Lisp-kurssista,
Prosessori 3/1983:sta:
*/
(defun *member (alkio lista)
(cond ((null lista) nil)
((equal (car lista) alkio) lista)
(t (*member alkio (cdr lista)))))
(defun *append (vasen oikea)
(cond ((null vasen) oikea)
(t (cons (car vasen)
(*append (cdr vasen) oikea)))))
(defun *remove (alkio lista)
(cond ((null lista) nil)
((equal alkio (car lista)) (*remove alkio (cdr lista)))
(t (cons (car lista)
(*remove alkio (cdr lista))))))
(defun *reverse (lista)
(cond ((null lista) nil)
(t (append (*reverse (cdr lista))
(cons (car lista) nil)))))
(defun *deepreverse (lista)
(cond ((atom lista) lista)
((null (cdr lista)) (cons (*deepreverse (car lista)) nil))
(t (append (*deepreverse (cdr lista))
(*deepreverse (cons (car lista) nil))))))
(defun latista (lista) (litistys lista nil))
(defun litistys (lista tulos)
(cond ((null lista) tulos)
((atom lista) (cons lista tulos))
(t (litistys (car lista)
(litistys (cdr lista) tulos)))))
(defun rewerse (lista)
(cond ((null (cdr lista)) lista)
(t (cons (car (rewerse (cdr lista)))
(rewerse (cons
(car lista)
(rewerse (cdr (rewerse (cdr lista))))))))))
; Count how many times rewerse is called when rewersing lista of length n:
(defun rewerse-count (n)
(cond ((lessp n 2) 1)
(t (add1
(+ (* 3 (rewerse-count (sub1 n)))
(rewerse-count (- n 2))
)
)
)
)
)