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.
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.
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/>.
16 (/debug "loading list.lisp!")
18 ;;;; Various list functions
20 (defun cons (x y) (cons x y))
21 (defun consp (x) (consp x))
24 (or (consp x) (null x)))
34 (error "The value `~S' is not a type list." x))))
37 "Return the CAR part of a cons, or NIL if X is null."
40 (defun cdr (x) (cdr x))
42 (defun rplaca (cons x)
45 (defun rplacd (cons x)
48 (defun first (x) (car x))
49 (defun second (x) (cadr x))
50 (defun third (x) (caddr x))
51 (defun fourth (x) (cadddr x))
52 (defun fifth (x) (car (cddddr x)))
53 (defun sixth (x) (cadr (cddddr x)))
54 (defun seventh (x) (caddr (cddddr x)))
55 (defun eighth (x) (cadddr (cddddr x)))
56 (defun ninth (x) (car (cddddr (cddddr x))))
57 (defun tenth (x) (cadr (cddddr (cddddr x))))
58 (defun rest (x) (cdr x))
60 (defun list (&rest args)
63 (defun list* (arg &rest others)
64 (cond ((null others) arg)
65 ((null (cdr others)) (cons arg (car others)))
66 (t (do ((x others (cdr x)))
67 ((null (cddr x)) (rplacd x (cadr x))))
70 (defun list-length (list)
72 (while (not (null list))
74 (setq list (cdr list)))
77 (defun nthcdr (n list)
78 (while (and (plusp n) list)
80 (setq list (cdr list)))
84 (car (nthcdr n list)))
86 (define-setf-expander nth (n list)
87 (let ((g!list (gensym))
90 (values (list g!list g!index)
93 `(rplaca (nthcdr ,g!index ,g!list) ,g!value)
94 `(nth ,g!index ,g!list))))
96 (defun caar (x) (car (car x)))
97 (defun cadr (x) (car (cdr x)))
98 (defun cdar (x) (cdr (car x)))
99 (defun cddr (x) (cdr (cdr x)))
101 (defun caaar (x) (car (caar x)))
102 (defun caadr (x) (car (cadr x)))
103 (defun cadar (x) (car (cdar x)))
104 (defun caddr (x) (car (cddr x)))
105 (defun cdaar (x) (cdr (caar x)))
106 (defun cdadr (x) (cdr (cadr x)))
107 (defun cddar (x) (cdr (cdar x)))
108 (defun cdddr (x) (cdr (cddr x)))
110 (defun caaaar (x) (car (caaar x)))
111 (defun caaadr (x) (car (caadr x)))
112 (defun caadar (x) (car (cadar x)))
113 (defun caaddr (x) (car (caddr x)))
114 (defun cadaar (x) (car (cdaar x)))
115 (defun cadadr (x) (car (cdadr x)))
116 (defun caddar (x) (car (cddar x)))
117 (defun cadddr (x) (car (cdddr x)))
118 (defun cdaaar (x) (cdr (caaar x)))
119 (defun cdaadr (x) (cdr (caadr x)))
120 (defun cdadar (x) (cdr (cadar x)))
121 (defun cdaddr (x) (cdr (caddr x)))
122 (defun cddaar (x) (cdr (cdaar x)))
123 (defun cddadr (x) (cdr (cdadr x)))
124 (defun cdddar (x) (cdr (cddar x)))
125 (defun cddddr (x) (cdr (cdddr x)))
127 (defun append-two (list1 list2)
131 (append (cdr list1) list2))))
133 (defun append (&rest lists)
134 (!reduce #'append-two lists nil))
136 (defun revappend (list1 list2)
138 (push (car list1) list2)
139 (setq list1 (cdr list1)))
142 (defun reverse (list)
143 (revappend list '()))
145 (defun sublis (alist tree &key key (test #'eql testp) (test-not #'eql test-not-p))
146 (when (and testp test-not-p)
147 (error "Both test and test-not are set"))
149 (let* ((key-val (if key (funcall key tree) tree))
150 (replace (if test-not-p
151 (assoc key-val alist :test-not test-not)
152 (assoc key-val alist :test test)))
153 (x (if replace (cdr replace) tree)))
156 (cons (s (car x)) (s (cdr x)))))))
159 (defun subst (new old tree &key key (test #'eql testp) (test-not #'eql test-not-p))
161 (cond ((satisfies-test-p old x :key key :test test :testp testp
162 :test-not test-not :test-not-p test-not-p)
165 (t (let ((a (s (car x)))
167 (if (and (eq a (car x))
174 (mapcar #'identity x))
176 (defun copy-tree (tree)
178 (cons (copy-tree (car tree))
179 (copy-tree (cdr tree)))
182 (defun tree-equal (tree1 tree2 &key (test #'eql testp)
183 (test-not #'eql test-not-p))
184 (when (and testp test-not-p) (error "Both test and test-not are set"))
185 (let ((func (if test-not-p (complement test-not) test)))
186 (labels ((%tree-equal (tree1 tree2)
188 (and (atom tree2) (funcall func tree1 tree2))
190 (%tree-equal (car tree1) (car tree2))
191 (%tree-equal (cdr tree1) (cdr tree2))))))
192 (%tree-equal tree1 tree2))))
194 (defun tailp (object list)
195 (do ((tail list (cdr tail)))
196 ((atom tail) (eq object tail))
197 (when (eql tail object)
198 (return-from tailp t))))
200 (defun map1 (func list)
203 (collect (funcall func (car list)))
204 (setq list (cdr list)))))
206 (defun mapcar (func list &rest lists)
207 (let ((lists (cons list lists)))
211 (let ((elems (map1 #'car lists)))
212 (do ((tail lists (cdr tail)))
214 (when (null (car tail)) (return-from loop))
215 (rplaca tail (cdar tail)))
216 (collect (apply func elems))))))))
218 (defun mapn (func list)
221 (collect (funcall func list))
222 (setq list (cdr list)))))
224 (defun maplist (func list &rest lists)
225 (let ((lists (cons list lists)))
229 (let ((elems (mapn #'car lists)))
230 (do ((tail lists (cdr tail)))
232 (when (null (car tail)) (return-from loop))
233 (rplaca tail (cdar tail)))
234 (collect (apply func elems))))))))
236 (defun mapc (func &rest lists)
237 (do* ((tails lists (map1 #'cdr tails))
238 (elems (map1 #'car tails)
240 ((dolist (x tails) (when (null x) (return t)))
245 (while (consp (cdr x))
251 (cons (car x) (butlast (cdr x)))))
253 (defun member (x list &key key (test #'eql testp) (test-not #'eql test-not-p))
255 (when (satisfies-test-p x (car list) :key key :test test :testp testp
256 :test-not test-not :test-not-p test-not-p)
258 (setq list (cdr list))))
261 (defun assoc (x alist &key key (test #'eql testp) (test-not #'eql test-not-p))
263 (if (satisfies-test-p x (caar alist) :key key :test test :testp testp
264 :test-not test-not :test-not-p test-not-p)
266 (setq alist (cdr alist))))
269 (defun rassoc (x alist &key key (test #'eql) (test #'eql testp)
270 (test-not #'eql test-not-p))
272 (if (satisfies-test-p x (cdar alist) :key key :test test :testp testp
273 :test-not test-not :test-not-p test-not-p)
275 (setq alist (cdr alist))))
278 (defun acons (key datum alist)
279 (cons (cons key datum) alist))
281 (defun pairlis (keys data &optional (alist ()))
283 (setq alist (acons (car keys) (car data) alist))
284 (setq keys (cdr keys))
285 (setq data (cdr data)))
288 (defun copy-alist (alist)
289 (let ((new-alist ()))
291 (push (cons (caar alist) (cdar alist)) new-alist)
292 (setq alist (cdr alist)))
293 (reverse new-alist)))
296 (define-setf-expander car (x)
297 (let ((cons (gensym))
298 (new-value (gensym)))
302 `(progn (rplaca ,cons ,new-value) ,new-value)
305 (define-setf-expander cdr (x)
306 (let ((cons (gensym))
307 (new-value (gensym)))
311 `(progn (rplacd ,cons ,new-value) ,new-value)
315 ;; The NCONC function is based on the SBCL's one.
316 (defun nconc (&rest lists)
317 (flet ((fail (object)
318 (error "type-error in nconc")))
319 (do ((top lists (cdr top)))
321 (let ((top-of-top (car top)))
324 (let* ((result top-of-top)
326 (do ((elements (cdr top) (cdr elements)))
328 (let ((ele (car elements)))
330 (cons (rplacd (last splice) ele)
332 (null (rplacd (last splice) nil))
333 (atom (if (cdr elements)
335 (rplacd (last splice) ele))))))
341 (return top-of-top))))))))
345 (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
346 (2nd x 1st) ; 2nd follows first down the list.
347 (3rd y 2nd)) ;3rd follows 2nd down the list.
352 (defun adjoin (item list &key (test #'eql) (key #'identity))
353 (if (member item list :key key :test test)
357 (defun intersection (list1 list2 &key (test #'eql) (key #'identity))
360 (when (member (funcall key x) list2 :test test :key key)