36adc0e9f4b647a870e951f7d4598a346dcc0d70
[jscl.git] / src / list.lisp
1 ;;; list.lisp --- 
2
3 ;; JSCL is free software: you can redistribute it and/or
4 ;; modify it under the terms of the GNU General Public License as
5 ;; published by the Free Software Foundation, either version 3 of the
6 ;; License, or (at your option) any later version.
7 ;;
8 ;; JSCL is distributed in the hope that it will be useful, but
9 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 ;; General Public License for more details.
12 ;;
13 ;; You should have received a copy of the GNU General Public License
14 ;; along with JSCL.  If not, see <http://www.gnu.org/licenses/>.
15
16 (/debug "loading list.lisp!")
17
18 ;;;; Various list functions
19
20 (defun cons (x y) (cons x y))
21 (defun consp (x) (consp x))
22
23 (defun listp (x)
24   (or (consp x) (null x)))
25
26 (defun null (x)
27   (eq x nil))
28
29 (defun endp (x)
30   (if (null x)
31       t
32       (if (consp x)
33           nil
34           (error "The value `~S' is not a type list." x))))
35
36 (defun car (x)
37   "Return the CAR part of a cons, or NIL if X is null."
38   (car x))
39
40 (defun cdr (x) (cdr x))
41
42 (defun first   (x) (car    x))
43 (defun second  (x) (cadr   x))
44 (defun third   (x) (caddr  x))
45 (defun fourth  (x) (cadddr x))
46 (defun fifth   (x) (car    (cddddr x)))
47 (defun sixth   (x) (cadr   (cddddr x)))
48 (defun seventh (x) (caddr  (cddddr x)))
49 (defun eighth  (x) (cadddr (cddddr x)))
50 (defun ninth   (x) (car  (cddddr (cddddr x))))
51 (defun tenth   (x) (cadr (cddddr (cddddr x))))
52 (defun rest    (x) (cdr x))
53
54 (defun list (&rest args)
55   args)
56
57 (defun list* (arg &rest others)
58   (cond ((null others) arg)
59         ((null (cdr others)) (cons arg (car others)))
60         (t (do ((x others (cdr x)))
61                ((null (cddr x)) (rplacd x (cadr x))))
62            (cons arg others))))
63
64 (defun nthcdr (n list)
65   (while (and (plusp n) list)
66     (setq n (1- n))
67     (setq list (cdr list)))
68   list)
69
70 (defun nth (n list)
71   (car (nthcdr n list)))
72
73 (define-setf-expander nth (n list)
74   (let ((g!list (gensym))
75         (g!index (gensym))
76         (g!value (gensym)))
77     (values (list g!list g!index)
78             (list list n)
79             (list g!value)
80             `(rplaca (nthcdr ,g!index ,g!list) ,g!value)
81             `(nth ,g!index ,g!list))))
82
83 (defun caar (x) (car (car x)))
84 (defun cadr (x) (car (cdr x)))
85 (defun cdar (x) (cdr (car x)))
86 (defun cddr (x) (cdr (cdr x)))
87
88 (defun caaar (x) (car (caar x)))
89 (defun caadr (x) (car (cadr x)))
90 (defun cadar (x) (car (cdar x)))
91 (defun caddr (x) (car (cddr x)))
92 (defun cdaar (x) (cdr (caar x)))
93 (defun cdadr (x) (cdr (cadr x)))
94 (defun cddar (x) (cdr (cdar x)))
95 (defun cdddr (x) (cdr (cddr x)))
96
97 (defun caaaar (x) (car (caaar x)))
98 (defun caaadr (x) (car (caadr x)))
99 (defun caadar (x) (car (cadar x)))
100 (defun caaddr (x) (car (caddr x)))
101 (defun cadaar (x) (car (cdaar x)))
102 (defun cadadr (x) (car (cdadr x)))
103 (defun caddar (x) (car (cddar x)))
104 (defun cadddr (x) (car (cdddr x)))
105 (defun cdaaar (x) (cdr (caaar x)))
106 (defun cdaadr (x) (cdr (caadr x)))
107 (defun cdadar (x) (cdr (cadar x)))
108 (defun cdaddr (x) (cdr (caddr x)))
109 (defun cddaar (x) (cdr (cdaar x)))
110 (defun cddadr (x) (cdr (cdadr x)))
111 (defun cdddar (x) (cdr (cddar x)))
112 (defun cddddr (x) (cdr (cdddr x)))
113
114 (defun sublis (alist tree &key key (test #'eql testp) (test-not #'eql test-not-p))
115   (when (and testp test-not-p)
116     (error "Both test and test-not are set"))
117   (labels ((s (tree)
118              (let* ((key-val (if key (funcall key tree) tree))
119                     (replace (if test-not-p
120                                  (assoc key-val alist :test-not test-not)
121                                  (assoc key-val alist :test test)))
122                     (x (if replace (cdr replace) tree)))
123                (if (atom x)
124                    x
125                    (cons (s (car x)) (s (cdr x)))))))
126     (s tree)))
127
128 (defun subst (new old tree &key key (test #'eql testp) (test-not #'eql test-not-p))
129   (labels ((s (x)
130              (cond ((satisfies-test-p old x :key key :test test :testp testp
131                                       :test-not test-not :test-not-p test-not-p)
132                     new)
133                    ((atom x) x)
134                    (t (let ((a (s (car x)))
135                             (b (s (cdr x))))
136                         (if (and (eq a (car x))
137                                  (eq b (cdr x)))
138                             x
139                             (cons a b)))))))
140     (s tree)))
141
142 (defun copy-list (x)
143   (mapcar #'identity x))
144
145 (defun copy-tree (tree)
146   (if (consp tree)
147     (cons (copy-tree (car tree))
148           (copy-tree (cdr tree)))
149     tree))
150
151 (defun tree-equal (tree1 tree2 &key (test #'eql testp)
152                          (test-not #'eql test-not-p))
153   (when (and testp test-not-p) (error "Both test and test-not are set"))
154   (let ((func (if test-not-p (complement test-not) test)))
155     (labels ((%tree-equal (tree1 tree2)
156                (if (atom tree1)
157                  (and (atom tree2) (funcall func tree1 tree2))
158                  (and (consp tree2)
159                       (%tree-equal (car tree1) (car tree2))
160                       (%tree-equal (cdr tree1) (cdr tree2))))))
161       (%tree-equal tree1 tree2))))
162
163 (defun tailp (object list)
164   (do ((tail list (cdr tail)))
165     ((atom tail) (eq object tail))
166     (when (eql tail object)
167       (return-from tailp t))))
168
169 (defmacro pop (place)
170   (multiple-value-bind (dummies vals newval setter getter)
171     (get-setf-expansion place)
172     (let ((head (gensym)))
173       `(let* (,@(mapcar #'list dummies vals) 
174               (,head ,getter)
175               (,(car newval) (cdr ,head))
176               ,@(cdr newval)) 
177          ,setter
178          (car ,head)))))
179
180
181 (defun map1 (func list)
182   (with-collect
183     (while list
184       (collect (funcall func (car list)))
185       (setq list (cdr list)))))
186
187 (defun mapcar (func list &rest lists)
188   (let ((lists (cons list lists)))
189     (with-collect
190       (block loop
191         (loop
192            (let ((elems (map1 #'car lists)))
193              (do ((tail lists (cdr tail)))
194                  ((null tail))
195                (when (null (car tail)) (return-from loop))
196                (rplaca tail (cdar tail)))
197              (collect (apply func elems))))))))
198
199 (defun mapn (func list)
200   (with-collect
201     (while list
202       (collect (funcall func list))
203       (setq list (cdr list)))))
204
205 (defun maplist (func list &rest lists)
206   (let ((lists (cons list lists)))
207     (with-collect
208       (block loop
209         (loop
210            (let ((elems (mapn #'car lists)))
211              (do ((tail lists (cdr tail)))
212                  ((null tail))
213                (when (null (car tail)) (return-from loop))
214                (rplaca tail (cdar tail)))
215              (collect (apply func elems))))))))
216
217 (defun mapc (func &rest lists)
218   (do* ((tails lists (map1 #'cdr tails))
219         (elems (map1 #'car tails)
220                (map1 #'car tails)))
221        ((dolist (x tails) (when (null x) (return t)))
222         (car lists))
223     (apply func elems)))
224
225 (defun last (x)
226   (while (consp (cdr x))
227     (setq x (cdr x)))
228   x)
229
230 (defun butlast (x)
231   (and (consp (cdr x))
232        (cons (car x) (butlast (cdr x)))))
233
234 (defun member (x list &key key (test #'eql testp) (test-not #'eql test-not-p)) 
235   (while list
236     (when (satisfies-test-p x (car list) :key key :test test :testp testp
237                             :test-not test-not :test-not-p test-not-p)
238       (return list))
239     (setq list (cdr list))))
240
241
242 (defun assoc (x alist &key key (test #'eql testp) (test-not #'eql test-not-p))
243   (while alist
244     (if (satisfies-test-p x (caar alist) :key key :test test :testp testp
245                           :test-not test-not :test-not-p test-not-p)
246       (return)
247       (setq alist (cdr alist))))
248   (car alist))
249
250 (defun rassoc (x alist &key key (test #'eql) (test #'eql testp)
251                  (test-not #'eql test-not-p))
252   (while alist
253     (if (satisfies-test-p x (cdar alist) :key key :test test :testp testp
254                           :test-not test-not :test-not-p test-not-p)
255       (return)
256       (setq alist (cdr alist))))
257   (car alist))
258
259 (defun acons (key datum alist)
260   (cons (cons key datum) alist))
261
262 (defun pairlis (keys data &optional (alist ()))
263   (while keys
264     (setq alist (acons (car keys) (car data) alist))
265     (setq keys (cdr keys))
266     (setq data (cdr data)))
267   alist)
268
269 (defun copy-alist (alist)
270   (let ((new-alist ()))
271     (while alist
272       (push (cons (caar alist) (cdar alist)) new-alist)
273       (setq alist (cdr alist)))
274     (reverse new-alist)))
275
276
277 (define-setf-expander car (x)
278   (let ((cons (gensym))
279         (new-value (gensym)))
280     (values (list cons)
281             (list x)
282             (list new-value)
283             `(progn (rplaca ,cons ,new-value) ,new-value)
284             `(car ,cons))))
285
286 (define-setf-expander cdr (x)
287   (let ((cons (gensym))
288         (new-value (gensym)))
289     (values (list cons)
290             (list x)
291             (list new-value)
292             `(progn (rplacd ,cons ,new-value) ,new-value)
293             `(cdr ,cons))))
294
295
296 ;; The NCONC function is based on the SBCL's one.
297 (defun nconc (&rest lists)
298   (flet ((fail (object)
299            (error "type-error in nconc")))
300     (do ((top lists (cdr top)))
301         ((null top) nil)
302       (let ((top-of-top (car top)))
303         (typecase top-of-top
304           (cons
305            (let* ((result top-of-top)
306                   (splice result))
307              (do ((elements (cdr top) (cdr elements)))
308                  ((endp elements))
309                (let ((ele (car elements)))
310                  (typecase ele
311                    (cons (rplacd (last splice) ele)
312                          (setf splice ele))
313                    (null (rplacd (last splice) nil))
314                    (atom (if (cdr elements)
315                              (fail ele)
316                              (rplacd (last splice) ele))))))
317              (return result)))
318           (null)
319           (atom
320            (if (cdr top)
321                (fail top-of-top)
322                (return top-of-top))))))))
323
324
325 (defun nreconc (x y)
326   (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
327        (2nd x 1st)                ; 2nd follows first down the list.
328        (3rd y 2nd))               ;3rd follows 2nd down the list.
329       ((atom 2nd) 3rd)
330     (rplacd 2nd 3rd)))
331
332
333 (defun adjoin (item list &key (test #'eql) (key #'identity))
334   (if (member item list :key key :test test)
335     list
336     (cons item list)))
337
338 (defun intersection (list1 list2 &key (test #'eql) (key #'identity))
339   (let ((new-list ()))
340     (dolist (x list1)
341       (when (member (funcall key x) list2 :test test :key key)
342         (push x new-list)))
343     new-list))