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)))
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)))))
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)))
104 (mapcar #'identity x))
106 (defun copy-tree (tree)
108 (cons (copy-tree (car tree))
109 (copy-tree (cdr tree)))
112 (defun tree-equal (tree1 tree2 &key (test #'eql))
114 (and (atom tree2) (funcall test tree1 tree2))
116 (tree-equal (car tree1) (car tree2) :test test)
117 (tree-equal (cdr tree1) (cdr tree2) :test test))))
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))))
125 (defun subst (new old tree &key (key #'identity) (test #'eql))
127 ((funcall test (funcall key tree) (funcall key old))
130 (cons (subst new old (car tree) :key key :test test)
131 (subst new old (cdr tree) :key key :test test)))
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)
140 (,(car newval) (cdr ,head))
146 (defun map1 (func list)
149 (collect (funcall func (car list)))
150 (setq list (cdr list)))))
152 (defun mapcar (func list &rest lists)
153 (let ((lists (cons list lists)))
157 (let ((elems (map1 #'car lists)))
158 (do ((tail lists (cdr tail)))
160 (when (null (car tail)) (return-from loop))
161 (rplaca tail (cdar tail)))
162 (collect (apply func elems))))))))
165 (while (consp (cdr x))
171 (cons (car x) (butlast (cdr x)))))
173 (defun member (x list &key (key #'identity) (test #'eql))
175 (when (funcall test x (funcall key (car list)))
177 (setq list (cdr list))))
180 (defun assoc (x alist &key (test #'eql))
182 (if (funcall test x (caar alist))
184 (setq alist (cdr alist))))
187 (defun rassoc (x alist &key (test #'eql))
189 (if (funcall test x (cdar alist))
191 (setq alist (cdr alist))))
194 (defun acons (key datum alist)
195 (cons (cons key datum) alist))
197 (defun pairlis (keys data &optional (alist ()))
199 (setq alist (acons (car keys) (car data) alist))
200 (setq keys (cdr keys))
201 (setq data (cdr data)))
204 (defun copy-alist (alist)
205 (let ((new-alist ()))
207 (push (cons (caar alist) (cdar alist)) new-alist)
208 (setq alist (cdr alist)))
209 (reverse new-alist)))
212 (define-setf-expander car (x)
213 (let ((cons (gensym))
214 (new-value (gensym)))
218 `(progn (rplaca ,cons ,new-value) ,new-value)
221 (define-setf-expander cdr (x)
222 (let ((cons (gensym))
223 (new-value (gensym)))
227 `(progn (rplacd ,cons ,new-value) ,new-value)
231 ;; The NCONC function is based on the SBCL's one.
232 (defun nconc (&rest lists)
233 (flet ((fail (object)
234 (error "type-error in nconc")))
235 (do ((top lists (cdr top)))
237 (let ((top-of-top (car top)))
240 (let* ((result top-of-top)
242 (do ((elements (cdr top) (cdr elements)))
244 (let ((ele (car elements)))
246 (cons (rplacd (last splice) ele)
248 (null (rplacd (last splice) nil))
249 (atom (if (cdr elements)
251 (rplacd (last splice) ele))))))
257 (return top-of-top))))))))
261 (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
262 (2nd x 1st) ; 2nd follows first down the list.
263 (3rd y 2nd)) ;3rd follows 2nd down the list.
268 (defun adjoin (item list &key (test #'eql) (key #'identity))
269 (if (member item list :key key :test test)
273 (defun intersection (list1 list2 &key (test #'eql) (key #'identity))
276 (when (member x list2 :test test :key key)