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 rest (x) (cdr x))
46 (defun list (&rest args)
49 (defun list* (arg &rest others)
50 (cond ((null others) arg)
51 ((null (cdr others)) (cons arg (car others)))
52 (t (do ((x others (cdr x)))
53 ((null (cddr x)) (rplacd x (cadr x))))
56 (defun nthcdr (n list)
57 (while (and (plusp n) list)
59 (setq list (cdr list)))
63 (car (nthcdr n list)))
65 ;;; The rest of the C[AD]*R functions; only a few were defined in boot.lisp
66 (defun caar (x) (car (car x)))
67 (defun cadr (x) (car (cdr x)))
68 (defun cdar (x) (cdr (car x)))
69 (defun cddr (x) (cdr (cdr x)))
70 (defun cadar (x) (car (cdr (car x))))
71 (defun caddr (x) (car (cdr (cdr x))))
72 (defun cdddr (x) (cdr (cdr (cdr x))))
73 (defun cadddr (x) (car (cdr (cdr (cdr x)))))
75 (defun cadar (x) (car (cdar x)))
76 (defun caaar (x) (car (caar x)))
77 (defun caadr (x) (car (cadr x)))
78 (defun cdaar (x) (cdr (caar x)))
79 (defun cdadr (x) (cdr (cadr x)))
80 (defun cddar (x) (cdr (cdar x)))
81 (defun caaaar (x) (car (caaar x)))
82 (defun caaadr (x) (car (caadr x)))
83 (defun caadar (x) (car (cadar x)))
84 (defun caaddr (x) (car (caddr x)))
85 (defun cadaar (x) (car (cdaar x)))
86 (defun cadadr (x) (car (cdadr x)))
87 (defun caddar (x) (car (cddar x)))
88 (defun cdaaar (x) (cdr (caaar x)))
89 (defun cdaadr (x) (cdr (caadr x)))
90 (defun cdadar (x) (cdr (cadar x)))
91 (defun cdaddr (x) (cdr (caddr x)))
92 (defun cddaar (x) (cdr (cdaar x)))
93 (defun cddadr (x) (cdr (cdadr x)))
94 (defun cdddar (x) (cdr (cddar x)))
95 (defun cddddr (x) (cdr (cdddr x)))
99 (mapcar #'identity x))
101 (defun copy-tree (tree)
103 (cons (copy-tree (car tree))
104 (copy-tree (cdr tree)))
107 (defun subst (new old tree &key (key #'identity) (test #'eql))
109 ((funcall test (funcall key tree) (funcall key old))
112 (cons (subst new old (car tree) :key key :test test)
113 (subst new old (cdr tree) :key key :test test)))
116 (defmacro pop (place)
117 (multiple-value-bind (dummies vals newval setter getter)
118 (get-setf-expansion place)
119 (let ((head (gensym)))
120 `(let* (,@(mapcar #'list dummies vals)
122 (,(car newval) (cdr ,head))
128 (defun map1 (func list)
131 (collect (funcall func (car list)))
132 (setq list (cdr list)))))
134 (defun mapcar (func list &rest lists)
135 (let ((lists (cons list lists)))
139 (let ((elems (map1 #'car lists)))
140 (do ((tail lists (cdr tail)))
142 (when (null (car tail)) (return-from loop))
143 (rplaca tail (cdar tail)))
144 (collect (apply func elems))))))))
147 (while (consp (cdr x))
153 (cons (car x) (butlast (cdr x)))))
155 (defun member (x list)
157 (when (eql x (car list))
159 (setq list (cdr list))))
162 (defun assoc (x alist &key (test #'eql))
164 (if (funcall test x (caar alist))
166 (setq alist (cdr alist))))
171 (define-setf-expander car (x)
172 (let ((cons (gensym))
173 (new-value (gensym)))
177 `(progn (rplaca ,cons ,new-value) ,new-value)
180 (define-setf-expander cdr (x)
181 (let ((cons (gensym))
182 (new-value (gensym)))
186 `(progn (rplacd ,cons ,new-value) ,new-value)
190 ;; The NCONC function is based on the SBCL's one.
191 (defun nconc (&rest lists)
192 (flet ((fail (object)
193 (error "type-error in nconc")))
194 (do ((top lists (cdr top)))
196 (let ((top-of-top (car top)))
199 (let* ((result top-of-top)
201 (do ((elements (cdr top) (cdr elements)))
203 (let ((ele (car elements)))
205 (cons (rplacd (last splice) ele)
207 (null (rplacd (last splice) nil))
208 (atom (if (cdr elements)
210 (rplacd (last splice) ele))))))
216 (return top-of-top))))))))
220 (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
221 (2nd x 1st) ; 2nd follows first down the list.
222 (3rd y 2nd)) ;3rd follows 2nd down the list.