32d03989b3623b43439e84a8226e6859354fa911
[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 (defun cadar (x) (car (cdr (car x))))
76 (defun caddr (x) (car (cdr (cdr x))))
77 (defun cdddr (x) (cdr (cdr (cdr x))))
78 (defun cadddr (x) (car (cdr (cdr (cdr x)))))
79
80 (defun cadar  (x) (car (cdar  x)))
81 (defun caaar  (x) (car (caar  x)))
82 (defun caadr  (x) (car (cadr  x)))
83 (defun cdaar  (x) (cdr (caar  x)))
84 (defun cdadr  (x) (cdr (cadr  x)))
85 (defun cddar  (x) (cdr (cdar  x)))
86 (defun caaaar (x) (car (caaar x)))
87 (defun caaadr (x) (car (caadr x)))
88 (defun caadar (x) (car (cadar x)))
89 (defun caaddr (x) (car (caddr x)))
90 (defun cadaar (x) (car (cdaar x)))
91 (defun cadadr (x) (car (cdadr x)))
92 (defun caddar (x) (car (cddar 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
103 (defun copy-list (x)
104   (mapcar #'identity x))
105
106 (defun copy-tree (tree)
107   (if (consp tree)
108     (cons (copy-tree (car tree))
109           (copy-tree (cdr tree)))
110     tree))
111
112 (defun tree-equal (tree1 tree2 &key (test #'eql))
113   (if (atom tree1)
114     (and (atom tree2) (funcall test tree1 tree2))
115     (and (consp tree2)
116          (tree-equal (car tree1) (car tree2) :test test)
117          (tree-equal (cdr tree1) (cdr tree2) :test test))))
118
119 (defun tailp (object list)
120   (do ((tail list (cdr tail)))
121     ((atom tail) (eq object tail))
122     (when (eql tail object)
123       (return-from tailp t))))
124
125 (defun subst (new old tree &key (key #'identity) (test #'eql))
126   (cond 
127     ((funcall test (funcall key tree) (funcall key old))
128      new) 
129     ((consp tree)
130      (cons (subst new old (car tree) :key key :test test)
131            (subst new old (cdr tree) :key key :test test))) 
132     (t tree)))
133
134 (defmacro pop (place)
135   (multiple-value-bind (dummies vals newval setter getter)
136     (get-setf-expansion place)
137     (let ((head (gensym)))
138       `(let* (,@(mapcar #'list dummies vals) 
139               (,head ,getter)
140               (,(car newval) (cdr ,head))
141               ,@(cdr newval)) 
142          ,setter
143          (car ,head)))))
144
145
146 (defun map1 (func list)
147   (with-collect
148     (while list
149       (collect (funcall func (car list)))
150       (setq list (cdr list)))))
151
152 (defun mapcar (func list &rest lists)
153   (let ((lists (cons list lists)))
154     (with-collect
155       (block loop
156         (loop
157            (let ((elems (map1 #'car lists)))
158              (do ((tail lists (cdr tail)))
159                  ((null tail))
160                (when (null (car tail)) (return-from loop))
161                (rplaca tail (cdar tail)))
162              (collect (apply func elems))))))))
163
164 (defun last (x)
165   (while (consp (cdr x))
166     (setq x (cdr x)))
167   x)
168
169 (defun butlast (x)
170   (and (consp (cdr x))
171        (cons (car x) (butlast (cdr x)))))
172
173 (defun member (x list)
174   (while list
175     (when (eql x (car list))
176       (return list))
177     (setq list (cdr list))))
178
179
180 (defun assoc (x alist &key (test #'eql))
181   (while alist
182     (if (funcall test x (caar alist))
183         (return)
184         (setq alist (cdr alist))))
185   (car alist))
186
187 (defun acons (key datum alist)
188   (cons (cons key datum) alist))
189
190 (defun pairlis (keys data &optional (alist ()))
191   (while keys
192     (setq alist (acons (car keys) (car data) alist))
193     (setq keys (cdr keys))
194     (setq data (cdr data)))
195   alist)
196
197 (defun copy-alist (alist)
198   (let ((new-alist ()))
199     (while alist
200       (push (cons (caar alist) (cdar alist)) new-alist)
201       (setq alist (cdr alist)))
202     (reverse new-alist)))
203
204
205 (define-setf-expander car (x)
206   (let ((cons (gensym))
207         (new-value (gensym)))
208     (values (list cons)
209             (list x)
210             (list new-value)
211             `(progn (rplaca ,cons ,new-value) ,new-value)
212             `(car ,cons))))
213
214 (define-setf-expander cdr (x)
215   (let ((cons (gensym))
216         (new-value (gensym)))
217     (values (list cons)
218             (list x)
219             (list new-value)
220             `(progn (rplacd ,cons ,new-value) ,new-value)
221             `(car ,cons))))
222
223
224 ;; The NCONC function is based on the SBCL's one.
225 (defun nconc (&rest lists)
226   (flet ((fail (object)
227            (error "type-error in nconc")))
228     (do ((top lists (cdr top)))
229         ((null top) nil)
230       (let ((top-of-top (car top)))
231         (typecase top-of-top
232           (cons
233            (let* ((result top-of-top)
234                   (splice result))
235              (do ((elements (cdr top) (cdr elements)))
236                  ((endp elements))
237                (let ((ele (car elements)))
238                  (typecase ele
239                    (cons (rplacd (last splice) ele)
240                          (setf splice ele))
241                    (null (rplacd (last splice) nil))
242                    (atom (if (cdr elements)
243                              (fail ele)
244                              (rplacd (last splice) ele))))))
245              (return result)))
246           (null)
247           (atom
248            (if (cdr top)
249                (fail top-of-top)
250                (return top-of-top))))))))
251
252
253 (defun nreconc (x y)
254   (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
255        (2nd x 1st)                ; 2nd follows first down the list.
256        (3rd y 2nd))               ;3rd follows 2nd down the list.
257       ((atom 2nd) 3rd)
258     (rplacd 2nd 3rd)))