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 ;;;; Various list functions
18 (defun cons (x y) (cons x y))
19 (defun consp (x) (consp x))
22 (or (consp x) (null x)))
32 (error "The value `~S' is not a type list." x))))
35 "Return the CAR part of a cons, or NIL if X is null."
38 (defun cdr (x) (cdr x))
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))
52 (defun list (&rest args)
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))))
62 (defun nthcdr (n list)
63 (while (and (plusp n) list)
65 (setq list (cdr list)))
69 (car (nthcdr n list)))
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)))
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)))
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)))
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"))
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)))
113 (cons (s (car x)) (s (cdr x)))))))
116 (defun subst (new old tree &key key (test #'eql testp) (test-not #'eql test-not-p))
118 (cond ((satisfies-test-p old x :key key :test test :testp testp
119 :test-not test-not :test-not-p test-not-p)
122 (t (let ((a (s (car x)))
124 (if (and (eq a (car x))
131 (mapcar #'identity x))
133 (defun copy-tree (tree)
135 (cons (copy-tree (car tree))
136 (copy-tree (cdr tree)))
139 (defun tree-equal (tree1 tree2 &key (test #'eql testp)
140 (test-not #'eql test-not-p))
141 (when (and testp test-not-p) (error "Both test and test-not are set"))
142 (let ((func (if test-not-p (complement test-not) test)))
143 (labels ((%tree-equal (tree1 tree2)
145 (and (atom tree2) (funcall func tree1 tree2))
147 (%tree-equal (car tree1) (car tree2))
148 (%tree-equal (cdr tree1) (cdr tree2))))))
149 (%tree-equal tree1 tree2))))
151 (defun tailp (object list)
152 (do ((tail list (cdr tail)))
153 ((atom tail) (eq object tail))
154 (when (eql tail object)
155 (return-from tailp t))))
157 (defmacro pop (place)
158 (multiple-value-bind (dummies vals newval setter getter)
159 (get-setf-expansion place)
160 (let ((head (gensym)))
161 `(let* (,@(mapcar #'list dummies vals)
163 (,(car newval) (cdr ,head))
169 (defun map1 (func list)
172 (collect (funcall func (car list)))
173 (setq list (cdr list)))))
175 (defun mapcar (func list &rest lists)
176 (let ((lists (cons list lists)))
180 (let ((elems (map1 #'car lists)))
181 (do ((tail lists (cdr tail)))
183 (when (null (car tail)) (return-from loop))
184 (rplaca tail (cdar tail)))
185 (collect (apply func elems))))))))
187 (defun mapc (func &rest lists)
188 (do* ((elems (map1 #'car lists) (map1 #'car lists-rest))
189 (lists-rest (map1 #'cdr lists) (map1 #'cdr lists-rest)))
190 ((dolist (x elems) (when (null x) (return t)))
195 (while (consp (cdr x))
201 (cons (car x) (butlast (cdr x)))))
203 (defun member (x list &key (key #'identity) (test #'eql))
205 (when (funcall test x (funcall key (car list)))
207 (setq list (cdr list))))
210 (defun assoc (x alist &key (test #'eql))
212 (if (funcall test x (caar alist))
214 (setq alist (cdr alist))))
217 (defun rassoc (x alist &key (test #'eql))
219 (if (funcall test x (cdar alist))
221 (setq alist (cdr alist))))
224 (defun acons (key datum alist)
225 (cons (cons key datum) alist))
227 (defun pairlis (keys data &optional (alist ()))
229 (setq alist (acons (car keys) (car data) alist))
230 (setq keys (cdr keys))
231 (setq data (cdr data)))
234 (defun copy-alist (alist)
235 (let ((new-alist ()))
237 (push (cons (caar alist) (cdar alist)) new-alist)
238 (setq alist (cdr alist)))
239 (reverse new-alist)))
242 (define-setf-expander car (x)
243 (let ((cons (gensym))
244 (new-value (gensym)))
248 `(progn (rplaca ,cons ,new-value) ,new-value)
251 (define-setf-expander cdr (x)
252 (let ((cons (gensym))
253 (new-value (gensym)))
257 `(progn (rplacd ,cons ,new-value) ,new-value)
261 ;; The NCONC function is based on the SBCL's one.
262 (defun nconc (&rest lists)
263 (flet ((fail (object)
264 (error "type-error in nconc")))
265 (do ((top lists (cdr top)))
267 (let ((top-of-top (car top)))
270 (let* ((result top-of-top)
272 (do ((elements (cdr top) (cdr elements)))
274 (let ((ele (car elements)))
276 (cons (rplacd (last splice) ele)
278 (null (rplacd (last splice) nil))
279 (atom (if (cdr elements)
281 (rplacd (last splice) ele))))))
287 (return top-of-top))))))))
291 (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
292 (2nd x 1st) ; 2nd follows first down the list.
293 (3rd y 2nd)) ;3rd follows 2nd down the list.
298 (defun adjoin (item list &key (test #'eql) (key #'identity))
299 (if (member item list :key key :test test)
303 (defun intersection (list1 list2 &key (test #'eql) (key #'identity))
306 (when (member x list2 :test test :key key)