FIFTH to TENTH
[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 subst (new old tree &key (key #'identity) (test #'eql))
120   (cond 
121     ((funcall test (funcall key tree) (funcall key old))
122      new) 
123     ((consp tree)
124      (cons (subst new old (car tree) :key key :test test)
125            (subst new old (cdr tree) :key key :test test))) 
126     (t tree)))
127
128 (defmacro pop (place)
129   (multiple-value-bind (dummies vals newval setter getter)
130     (get-setf-expansion place)
131     (let ((head (gensym)))
132       `(let* (,@(mapcar #'list dummies vals) 
133               (,head ,getter)
134               (,(car newval) (cdr ,head))
135               ,@(cdr newval)) 
136          ,setter
137          (car ,head)))))
138
139
140 (defun map1 (func list)
141   (with-collect
142     (while list
143       (collect (funcall func (car list)))
144       (setq list (cdr list)))))
145
146 (defun mapcar (func list &rest lists)
147   (let ((lists (cons list lists)))
148     (with-collect
149       (block loop
150         (loop
151            (let ((elems (map1 #'car lists)))
152              (do ((tail lists (cdr tail)))
153                  ((null tail))
154                (when (null (car tail)) (return-from loop))
155                (rplaca tail (cdar tail)))
156              (collect (apply func elems))))))))
157
158 (defun last (x)
159   (while (consp (cdr x))
160     (setq x (cdr x)))
161   x)
162
163 (defun butlast (x)
164   (and (consp (cdr x))
165        (cons (car x) (butlast (cdr x)))))
166
167 (defun member (x list)
168   (while list
169     (when (eql x (car list))
170       (return list))
171     (setq list (cdr list))))
172
173
174 (defun assoc (x alist &key (test #'eql))
175   (while alist
176     (if (funcall test x (caar alist))
177         (return)
178         (setq alist (cdr alist))))
179   (car alist))
180
181
182
183 (define-setf-expander car (x)
184   (let ((cons (gensym))
185         (new-value (gensym)))
186     (values (list cons)
187             (list x)
188             (list new-value)
189             `(progn (rplaca ,cons ,new-value) ,new-value)
190             `(car ,cons))))
191
192 (define-setf-expander cdr (x)
193   (let ((cons (gensym))
194         (new-value (gensym)))
195     (values (list cons)
196             (list x)
197             (list new-value)
198             `(progn (rplacd ,cons ,new-value) ,new-value)
199             `(car ,cons))))
200
201
202 ;; The NCONC function is based on the SBCL's one.
203 (defun nconc (&rest lists)
204   (flet ((fail (object)
205            (error "type-error in nconc")))
206     (do ((top lists (cdr top)))
207         ((null top) nil)
208       (let ((top-of-top (car top)))
209         (typecase top-of-top
210           (cons
211            (let* ((result top-of-top)
212                   (splice result))
213              (do ((elements (cdr top) (cdr elements)))
214                  ((endp elements))
215                (let ((ele (car elements)))
216                  (typecase ele
217                    (cons (rplacd (last splice) ele)
218                          (setf splice ele))
219                    (null (rplacd (last splice) nil))
220                    (atom (if (cdr elements)
221                              (fail ele)
222                              (rplacd (last splice) ele))))))
223              (return result)))
224           (null)
225           (atom
226            (if (cdr top)
227                (fail top-of-top)
228                (return top-of-top))))))))
229
230
231 (defun nreconc (x y)
232   (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
233        (2nd x 1st)                ; 2nd follows first down the list.
234        (3rd y 2nd))               ;3rd follows 2nd down the list.
235       ((atom 2nd) 3rd)
236     (rplacd 2nd 3rd)))