fd9e0bd51457483ad5b54fa7126fce3ddea7e9b2
[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 rest (x) (cdr x))
45
46 (defun list (&rest args)
47   args)
48
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))))
54            (cons arg others))))
55
56 (defun nthcdr (n list)
57   (while (and (plusp n) list)
58     (setq n (1- n))
59     (setq list (cdr list)))
60   list)
61
62 (defun nth (n list)
63   (car (nthcdr n list)))
64
65 (defun caar (x) (car (car x)))
66 (defun cadr (x) (car (cdr x)))
67 (defun cdar (x) (cdr (car x)))
68 (defun cddr (x) (cdr (cdr x)))
69 (defun cadar (x) (car (cdr (car x))))
70 (defun caddr (x) (car (cdr (cdr x))))
71 (defun cdddr (x) (cdr (cdr (cdr x))))
72 (defun cadddr (x) (car (cdr (cdr (cdr x)))))
73
74 (defun cadar  (x) (car (cdar  x)))
75 (defun caaar  (x) (car (caar  x)))
76 (defun caadr  (x) (car (cadr  x)))
77 (defun cdaar  (x) (cdr (caar  x)))
78 (defun cdadr  (x) (cdr (cadr  x)))
79 (defun cddar  (x) (cdr (cdar  x)))
80 (defun caaaar (x) (car (caaar x)))
81 (defun caaadr (x) (car (caadr x)))
82 (defun caadar (x) (car (cadar x)))
83 (defun caaddr (x) (car (caddr x)))
84 (defun cadaar (x) (car (cdaar x)))
85 (defun cadadr (x) (car (cdadr x)))
86 (defun caddar (x) (car (cddar x)))
87 (defun cdaaar (x) (cdr (caaar x)))
88 (defun cdaadr (x) (cdr (caadr x)))
89 (defun cdadar (x) (cdr (cadar x)))
90 (defun cdaddr (x) (cdr (caddr x)))
91 (defun cddaar (x) (cdr (cdaar x)))
92 (defun cddadr (x) (cdr (cdadr x)))
93 (defun cdddar (x) (cdr (cddar x)))
94 (defun cddddr (x) (cdr (cdddr x)))
95
96
97 (defun copy-list (x)
98   (mapcar #'identity x))
99
100 (defun copy-tree (tree)
101   (if (consp tree)
102     (cons (copy-tree (car tree))
103           (copy-tree (cdr tree)))
104     tree))
105
106 (defun tree-equal (tree1 tree2 &key (test #'eql))
107   (if (atom tree1)
108     (and (atom tree2) (funcall test tree1 tree2))
109     (and (consp tree2)
110          (tree-equal (car tree1) (car tree2) :test test)
111          (tree-equal (cdr tree1) (cdr tree2) :test test))))
112
113 (defun subst (new old tree &key (key #'identity) (test #'eql))
114   (cond 
115     ((funcall test (funcall key tree) (funcall key old))
116      new) 
117     ((consp tree)
118      (cons (subst new old (car tree) :key key :test test)
119            (subst new old (cdr tree) :key key :test test))) 
120     (t tree)))
121
122 (defmacro pop (place)
123   (multiple-value-bind (dummies vals newval setter getter)
124     (get-setf-expansion place)
125     (let ((head (gensym)))
126       `(let* (,@(mapcar #'list dummies vals) 
127               (,head ,getter)
128               (,(car newval) (cdr ,head))
129               ,@(cdr newval)) 
130          ,setter
131          (car ,head)))))
132
133
134 (defun map1 (func list)
135   (with-collect
136     (while list
137       (collect (funcall func (car list)))
138       (setq list (cdr list)))))
139
140 (defun mapcar (func list &rest lists)
141   (let ((lists (cons list lists)))
142     (with-collect
143       (block loop
144         (loop
145            (let ((elems (map1 #'car lists)))
146              (do ((tail lists (cdr tail)))
147                  ((null tail))
148                (when (null (car tail)) (return-from loop))
149                (rplaca tail (cdar tail)))
150              (collect (apply func elems))))))))
151
152 (defun last (x)
153   (while (consp (cdr x))
154     (setq x (cdr x)))
155   x)
156
157 (defun butlast (x)
158   (and (consp (cdr x))
159        (cons (car x) (butlast (cdr x)))))
160
161 (defun member (x list)
162   (while list
163     (when (eql x (car list))
164       (return list))
165     (setq list (cdr list))))
166
167
168 (defun assoc (x alist &key (test #'eql))
169   (while alist
170     (if (funcall test x (caar alist))
171         (return)
172         (setq alist (cdr alist))))
173   (car alist))
174
175
176
177 (define-setf-expander car (x)
178   (let ((cons (gensym))
179         (new-value (gensym)))
180     (values (list cons)
181             (list x)
182             (list new-value)
183             `(progn (rplaca ,cons ,new-value) ,new-value)
184             `(car ,cons))))
185
186 (define-setf-expander cdr (x)
187   (let ((cons (gensym))
188         (new-value (gensym)))
189     (values (list cons)
190             (list x)
191             (list new-value)
192             `(progn (rplacd ,cons ,new-value) ,new-value)
193             `(car ,cons))))
194
195
196 ;; The NCONC function is based on the SBCL's one.
197 (defun nconc (&rest lists)
198   (flet ((fail (object)
199            (error "type-error in nconc")))
200     (do ((top lists (cdr top)))
201         ((null top) nil)
202       (let ((top-of-top (car top)))
203         (typecase top-of-top
204           (cons
205            (let* ((result top-of-top)
206                   (splice result))
207              (do ((elements (cdr top) (cdr elements)))
208                  ((endp elements))
209                (let ((ele (car elements)))
210                  (typecase ele
211                    (cons (rplacd (last splice) ele)
212                          (setf splice ele))
213                    (null (rplacd (last splice) nil))
214                    (atom (if (cdr elements)
215                              (fail ele)
216                              (rplacd (last splice) ele))))))
217              (return result)))
218           (null)
219           (atom
220            (if (cdr top)
221                (fail top-of-top)
222                (return top-of-top))))))))
223
224
225 (defun nreconc (x y)
226   (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
227        (2nd x 1st)                ; 2nd follows first down the list.
228        (3rd y 2nd))               ;3rd follows 2nd down the list.
229       ((atom 2nd) 3rd)
230     (rplacd 2nd 3rd)))