Do not upcase variable names JS identifiers
[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* ((elems (map1 #'car lists) (map1 #'car lists-rest))
219         (lists-rest (map1 #'cdr lists) (map1 #'cdr lists-rest)))
220        ((dolist (x elems) (when (null x) (return t)))
221         (car lists))
222     (apply func elems)))
223
224 (defun last (x)
225   (while (consp (cdr x))
226     (setq x (cdr x)))
227   x)
228
229 (defun butlast (x)
230   (and (consp (cdr x))
231        (cons (car x) (butlast (cdr x)))))
232
233 (defun member (x list &key key (test #'eql testp) (test-not #'eql test-not-p)) 
234   (while list
235     (when (satisfies-test-p x (car list) :key key :test test :testp testp
236                             :test-not test-not :test-not-p test-not-p)
237       (return list))
238     (setq list (cdr list))))
239
240
241 (defun assoc (x alist &key key (test #'eql testp) (test-not #'eql test-not-p))
242   (while alist
243     (if (satisfies-test-p x (caar alist) :key key :test test :testp testp
244                           :test-not test-not :test-not-p test-not-p)
245       (return)
246       (setq alist (cdr alist))))
247   (car alist))
248
249 (defun rassoc (x alist &key key (test #'eql) (test #'eql testp)
250                  (test-not #'eql test-not-p))
251   (while alist
252     (if (satisfies-test-p x (cdar alist) :key key :test test :testp testp
253                           :test-not test-not :test-not-p test-not-p)
254       (return)
255       (setq alist (cdr alist))))
256   (car alist))
257
258 (defun acons (key datum alist)
259   (cons (cons key datum) alist))
260
261 (defun pairlis (keys data &optional (alist ()))
262   (while keys
263     (setq alist (acons (car keys) (car data) alist))
264     (setq keys (cdr keys))
265     (setq data (cdr data)))
266   alist)
267
268 (defun copy-alist (alist)
269   (let ((new-alist ()))
270     (while alist
271       (push (cons (caar alist) (cdar alist)) new-alist)
272       (setq alist (cdr alist)))
273     (reverse new-alist)))
274
275
276 (define-setf-expander car (x)
277   (let ((cons (gensym))
278         (new-value (gensym)))
279     (values (list cons)
280             (list x)
281             (list new-value)
282             `(progn (rplaca ,cons ,new-value) ,new-value)
283             `(car ,cons))))
284
285 (define-setf-expander cdr (x)
286   (let ((cons (gensym))
287         (new-value (gensym)))
288     (values (list cons)
289             (list x)
290             (list new-value)
291             `(progn (rplacd ,cons ,new-value) ,new-value)
292             `(cdr ,cons))))
293
294
295 ;; The NCONC function is based on the SBCL's one.
296 (defun nconc (&rest lists)
297   (flet ((fail (object)
298            (error "type-error in nconc")))
299     (do ((top lists (cdr top)))
300         ((null top) nil)
301       (let ((top-of-top (car top)))
302         (typecase top-of-top
303           (cons
304            (let* ((result top-of-top)
305                   (splice result))
306              (do ((elements (cdr top) (cdr elements)))
307                  ((endp elements))
308                (let ((ele (car elements)))
309                  (typecase ele
310                    (cons (rplacd (last splice) ele)
311                          (setf splice ele))
312                    (null (rplacd (last splice) nil))
313                    (atom (if (cdr elements)
314                              (fail ele)
315                              (rplacd (last splice) ele))))))
316              (return result)))
317           (null)
318           (atom
319            (if (cdr top)
320                (fail top-of-top)
321                (return top-of-top))))))))
322
323
324 (defun nreconc (x y)
325   (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
326        (2nd x 1st)                ; 2nd follows first down the list.
327        (3rd y 2nd))               ;3rd follows 2nd down the list.
328       ((atom 2nd) 3rd)
329     (rplacd 2nd 3rd)))
330
331
332 (defun adjoin (item list &key (test #'eql) (key #'identity))
333   (if (member item list :key key :test test)
334     list
335     (cons item list)))
336
337 (defun intersection (list1 list2 &key (test #'eql) (key #'identity))
338   (let ((new-list ()))
339     (dolist (x list1)
340       (when (member (funcall key x) list2 :test test :key key)
341         (push x new-list)))
342     new-list))