Add more exercices functions
[TD_LISP.git] / exercices / lists.lsp
1 #!/usr/bin/env newlisp
2
3 (setq L '(1 9 3 7 0 5))
4 (setq M '(2 6 5 4 0 1 3))
5
6 (define (mem E L) ;la function mem a 2 arguments
7 (if (null? L) nil
8 (if (= (first L) E) true
9 (mem E (rest L)))))
10 (println (mem 9 L))
11
12 (define (boolmemrec E L)
13 (cond
14 ((null? L) nil)
15 ((= (first L) E) true)
16 ((mem E (rest L)))))
17 (println (boolmemrec 10 L))
18
19 (define (mem E L) ;la function mem a 2 arguments
20 (if (null? L) nil
21 (if (= (first L) E) L
22 (mem E (rest L)))))
23 (println (mem 3 L))
24
25 (define (mem E L)
26 (cond
27 ((null? L) nil)
28 ((= (first L) E) L)
29 ((mem E (rest L)))))
30 (println (mem 9 L))
31 (println (member 9 L))
32 (println (mem 8 L))
33 (println (member 8 L))
34
35 (define (concatene L1 L2)
36 (if (null? L1) L2
37 (cons (first L1) (concatene (rest L1) L2))))
38 (setq C (concatene L M))
39 (println C)
40 (println (append L M))
41
42 ;(trace true)
43
44 (define (rang x L)
45 (if (boolmemrec x L)
46 (cond
47 ((= x (first L)) 0)
48 ((+ 1 (rang x (rest L)))))
49 nil))
50 (println (rang 4 L))
51 (println (find 4 L))
52 (println (rang 0 L))
53 (println (find 0 L))
54
55 ;(trace nil)
56
57 (define (tete n L)
58 (cond
59 ((null? L) '())
60 ((= n 0) '())
61 ((cons (first L) (tete (- n 1) (rest L))))))
62 (println (tete 3 L))
63 (println (slice L 0 3))
64 (println (0 3 L))
65
66 (define (inter L1 L2)
67 (cond
68 ((null? L1) '())
69 ((member (first L1) L2) (cons (first L1) (inter (rest L1) L2)))
70 ((inter (rest L1) L2))))
71 (println (inter L M))
72 (println (intersect L M))
73
74 (setq N '(1 9 3))
75 (define (inclu L1 L2)
76 (cond
77 ((null? L1) true)
78 ((null? L2) nil)
79 ((= (first L1) (first L2)) (inclu (rest L1) (rest L2)))
80 ((inclu L1 (rest L2)))))
81 (println (inclu N L))
82 (println (inclu L M))
83 ;did not found a newlisp builtin equivalent function
84
85 (define (unionE L1 L2)
86 (cond
87 ((null? L1) L2)
88 ((member (first L1) L2) (unionE (rest L1) L2))
89 ((cons (first L1) (unionE (rest L1) L2)))))
90 (println (unionE L M))
91 (println (unionE N L))
92
93 ;(trace true)
94
95 (setq T '(((4)((2 5))((((((6 7)))))))))
96 (define (prof L)
97 (cond
98 ((null? L) 0)
99 ((atom? L) 0)
100 ((max (+ 1 (prof (first L))) (prof (rest L))))))
101 (println (prof L))
102 (println (prof T))
103
104 ;(trace nil)
105 ;(trace true)
106
107 (define (aplatir L)
108 (cond
109 ((null? L) nil)
110 ((atom? L) (list L)) ;FIXME: the "casting" is not working properly
111 ((append (aplatir (first L)) (aplatir (rest L))))))
112 ;(println (aplatir T))
113 (println (flat T))
114
115 ;(trace nil)
116
117 (define (elim L)
118 (cond
119 ((null? L) '())
120 ((member (first L) (rest L)) (elim (rest L)))
121 ((cons (first L) (elim (rest L))))))
122 (println (elim C))
123 (println (unique C))
124
125 (define (reverseL L)
126 (cond
127 ((null? L) '())
128 ((append (reverseL (rest L)) (list (first L))))))
129 (println (reverseL L))
130 (println (reverse L))
131
132 (define (list< n L)
133 (cond
134 ((null? L) '())
135 ((>= (first L) n) (list< n (rest L)))
136 ((cons (first L) (list< n (rest L))))))
137 (println (list< 5 L))
138 (println (list< 5 C))
139
140 (define (list>= n L)
141 (cond
142 ((null? L) '())
143 ((< (first L) n) (list>= n (rest L)))
144 ((cons (first L) (list>= n (rest L))))))
145 (println (list>= 5 C))
146
147 (exit)