Modify SUBST to:
[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 ;;;; Various list functions
17
18 (defun cons (x y ) (cons x y))
19 (defun consp (x) (consp x))
20
21 (defun listp (x)
22   (or (consp x) (null x)))
23
24 (defun null (x)
25   (eq x nil))
26
27 (defun endp (x)
28   (if (null x)
29       t
30       (if (consp x)
31           nil
32           (error "The value `~S' is not a type list." x))))
33
34 (defun car (x)
35   "Return the CAR part of a cons, or NIL if X is null."
36   (car x))
37
38 (defun cdr (x) (cdr x))
39
40 (defun first   (x) (car    x))
41 (defun second  (x) (cadr   x))
42 (defun third   (x) (caddr  x))
43 (defun fourth  (x) (cadddr x))
44 (defun fifth   (x) (car    (cddddr x)))
45 (defun sixth   (x) (cadr   (cddddr x)))
46 (defun seventh (x) (caddr  (cddddr x)))
47 (defun eighth  (x) (cadddr (cddddr x)))
48 (defun ninth   (x) (car  (cddddr (cddddr x))))
49 (defun tenth   (x) (cadr (cddddr (cddddr x))))
50 (defun rest    (x) (cdr x))
51
52 (defun list (&rest args)
53   args)
54
55 (defun list* (arg &rest others)
56   (cond ((null others) arg)
57         ((null (cdr others)) (cons arg (car others)))
58         (t (do ((x others (cdr x)))
59                ((null (cddr x)) (rplacd x (cadr x))))
60            (cons arg others))))
61
62 (defun nthcdr (n list)
63   (while (and (plusp n) list)
64     (setq n (1- n))
65     (setq list (cdr list)))
66   list)
67
68 (defun nth (n list)
69   (car (nthcdr n list)))
70
71 (defun caar (x) (car (car x)))
72 (defun cadr (x) (car (cdr x)))
73 (defun cdar (x) (cdr (car x)))
74 (defun cddr (x) (cdr (cdr x)))
75
76 (defun caaar (x) (car (caar x)))
77 (defun caadr (x) (car (cadr x)))
78 (defun cadar (x) (car (cdar x)))
79 (defun caddr (x) (car (cddr x)))
80 (defun cdaar (x) (cdr (caar x)))
81 (defun cdadr (x) (cdr (cadr x)))
82 (defun cddar (x) (cdr (cdar x)))
83 (defun cdddr (x) (cdr (cddr x)))
84
85 (defun caaaar (x) (car (caaar x)))
86 (defun caaadr (x) (car (caadr x)))
87 (defun caadar (x) (car (cadar x)))
88 (defun caaddr (x) (car (caddr x)))
89 (defun cadaar (x) (car (cdaar x)))
90 (defun cadadr (x) (car (cdadr x)))
91 (defun caddar (x) (car (cddar x)))
92 (defun cadddr (x) (car (cdddr x)))
93 (defun cdaaar (x) (cdr (caaar x)))
94 (defun cdaadr (x) (cdr (caadr x)))
95 (defun cdadar (x) (cdr (cadar x)))
96 (defun cdaddr (x) (cdr (caddr x)))
97 (defun cddaar (x) (cdr (cdaar x)))
98 (defun cddadr (x) (cdr (cdadr x)))
99 (defun cdddar (x) (cdr (cddar x)))
100 (defun cddddr (x) (cdr (cdddr x)))
101
102 (defun sublis (alist tree &key key (test #'eql testp) (test-not #'eql test-not-p))
103   (when (and testp test-not-p)
104     (error "Both test and test-not are set"))
105   (labels ((s (tree)
106              (let* ((key-val (if key (funcall key tree) tree))
107                     (replace (if test-not-p
108                                  (assoc key-val alist :test-not test-not)
109                                  (assoc key-val alist :test test)))
110                     (x (if replace (cdr replace) tree)))
111                (if (atom x)
112                    x
113                    (cons (s (car x)) (s (cdr x)))))))
114     (s tree)))
115
116 (defun subst (new old tree &key key (test #'eql testp) (test-not #'eql test-not-p))
117   (labels ((s (x)
118              (cond ((satisfies-test-p old x :key key :test test :testp testp
119                                       :test-not test-not :test-not-p test-not-p)
120                     new)
121                    ((atom x) x)
122                    (t (let ((a (s (car x)))
123                             (b (s (cdr x))))
124                         (if (and (eq a (car x))
125                                  (eq b (cdr x)))
126                             x
127                             (cons a b)))))))
128     (s tree)))
129
130 (defun copy-list (x)
131   (mapcar #'identity x))
132
133 (defun copy-tree (tree)
134   (if (consp tree)
135     (cons (copy-tree (car tree))
136           (copy-tree (cdr tree)))
137     tree))
138
139 (defun tree-equal (tree1 tree2 &key (test #'eql))
140   (if (atom tree1)
141     (and (atom tree2) (funcall test tree1 tree2))
142     (and (consp tree2)
143          (tree-equal (car tree1) (car tree2) :test test)
144          (tree-equal (cdr tree1) (cdr tree2) :test test))))
145
146 (defun tailp (object list)
147   (do ((tail list (cdr tail)))
148     ((atom tail) (eq object tail))
149     (when (eql tail object)
150       (return-from tailp t))))
151
152 (defmacro pop (place)
153   (multiple-value-bind (dummies vals newval setter getter)
154     (get-setf-expansion place)
155     (let ((head (gensym)))
156       `(let* (,@(mapcar #'list dummies vals) 
157               (,head ,getter)
158               (,(car newval) (cdr ,head))
159               ,@(cdr newval)) 
160          ,setter
161          (car ,head)))))
162
163
164 (defun map1 (func list)
165   (with-collect
166     (while list
167       (collect (funcall func (car list)))
168       (setq list (cdr list)))))
169
170 (defun mapcar (func list &rest lists)
171   (let ((lists (cons list lists)))
172     (with-collect
173       (block loop
174         (loop
175            (let ((elems (map1 #'car lists)))
176              (do ((tail lists (cdr tail)))
177                  ((null tail))
178                (when (null (car tail)) (return-from loop))
179                (rplaca tail (cdar tail)))
180              (collect (apply func elems))))))))
181
182 (defun mapc (func &rest lists)
183   (do* ((elems (map1 #'car lists) (map1 #'car lists-rest))
184         (lists-rest (map1 #'cdr lists) (map1 #'cdr lists-rest)))
185        ((dolist (x elems) (when (null x) (return t)))
186         (car lists))
187     (apply func elems)))
188
189 (defun last (x)
190   (while (consp (cdr x))
191     (setq x (cdr x)))
192   x)
193
194 (defun butlast (x)
195   (and (consp (cdr x))
196        (cons (car x) (butlast (cdr x)))))
197
198 (defun member (x list &key (key #'identity) (test #'eql))
199   (while list
200     (when (funcall test x (funcall key (car list)))
201       (return list))
202     (setq list (cdr list))))
203
204
205 (defun assoc (x alist &key (test #'eql))
206   (while alist
207     (if (funcall test x (caar alist))
208       (return)
209       (setq alist (cdr alist))))
210   (car alist))
211
212 (defun rassoc (x alist &key (test #'eql))
213   (while alist
214     (if (funcall test x (cdar alist))
215       (return)
216       (setq alist (cdr alist))))
217   (car alist))
218
219 (defun acons (key datum alist)
220   (cons (cons key datum) alist))
221
222 (defun pairlis (keys data &optional (alist ()))
223   (while keys
224     (setq alist (acons (car keys) (car data) alist))
225     (setq keys (cdr keys))
226     (setq data (cdr data)))
227   alist)
228
229 (defun copy-alist (alist)
230   (let ((new-alist ()))
231     (while alist
232       (push (cons (caar alist) (cdar alist)) new-alist)
233       (setq alist (cdr alist)))
234     (reverse new-alist)))
235
236
237 (define-setf-expander car (x)
238   (let ((cons (gensym))
239         (new-value (gensym)))
240     (values (list cons)
241             (list x)
242             (list new-value)
243             `(progn (rplaca ,cons ,new-value) ,new-value)
244             `(car ,cons))))
245
246 (define-setf-expander cdr (x)
247   (let ((cons (gensym))
248         (new-value (gensym)))
249     (values (list cons)
250             (list x)
251             (list new-value)
252             `(progn (rplacd ,cons ,new-value) ,new-value)
253             `(car ,cons))))
254
255
256 ;; The NCONC function is based on the SBCL's one.
257 (defun nconc (&rest lists)
258   (flet ((fail (object)
259            (error "type-error in nconc")))
260     (do ((top lists (cdr top)))
261         ((null top) nil)
262       (let ((top-of-top (car top)))
263         (typecase top-of-top
264           (cons
265            (let* ((result top-of-top)
266                   (splice result))
267              (do ((elements (cdr top) (cdr elements)))
268                  ((endp elements))
269                (let ((ele (car elements)))
270                  (typecase ele
271                    (cons (rplacd (last splice) ele)
272                          (setf splice ele))
273                    (null (rplacd (last splice) nil))
274                    (atom (if (cdr elements)
275                              (fail ele)
276                              (rplacd (last splice) ele))))))
277              (return result)))
278           (null)
279           (atom
280            (if (cdr top)
281                (fail top-of-top)
282                (return top-of-top))))))))
283
284
285 (defun nreconc (x y)
286   (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
287        (2nd x 1st)                ; 2nd follows first down the list.
288        (3rd y 2nd))               ;3rd follows 2nd down the list.
289       ((atom 2nd) 3rd)
290     (rplacd 2nd 3rd)))
291
292
293 (defun adjoin (item list &key (test #'eql) (key #'identity))
294   (if (member item list :key key :test test)
295     list
296     (cons item list)))
297
298 (defun intersection (list1 list2 &key (test #'eql) (key #'identity))
299   (let ((new-list ()))
300     (dolist (x list1)
301       (when (member x list2 :test test :key key)
302         (push x new-list)))
303     new-list))