* read.c (scm_lreadr): When user-defined hash procedure returns
[bpt/guile.git] / ice-9 / common-list.scm
CommitLineData
6a4d3cfd
JB
1;;;; common-list.scm --- COMMON LISP list functions for Scheme
2;;;;
c771038b
TTN
3;;;; Copyright (C) 1995, 1996, 1997, 2001 Free Software Foundation, Inc.
4;;;;
6a4d3cfd
JB
5;;;; This program is free software; you can redistribute it and/or modify
6;;;; it under the terms of the GNU General Public License as published by
7;;;; the Free Software Foundation; either version 2, or (at your option)
8;;;; any later version.
c771038b 9;;;;
6a4d3cfd
JB
10;;;; This program is distributed in the hope that it will be useful,
11;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13;;;; GNU General Public License for more details.
c771038b 14;;;;
6a4d3cfd
JB
15;;;; You should have received a copy of the GNU General Public License
16;;;; along with this software; see the file COPYING. If not, write to
17;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18;;;; Boston, MA 02111-1307 USA
a482f2cc
MV
19;;;;
20;;;; As a special exception, the Free Software Foundation gives permission
21;;;; for additional uses of the text contained in its release of GUILE.
22;;;;
23;;;; The exception is that, if you link the GUILE library with other files
24;;;; to produce an executable, this does not by itself cause the
25;;;; resulting executable to be covered by the GNU General Public License.
26;;;; Your use of that executable is in no way restricted on account of
27;;;; linking the GUILE library code into it.
28;;;;
29;;;; This exception does not however invalidate any other reasons why
30;;;; the executable file might be covered by the GNU General Public License.
31;;;;
32;;;; This exception applies only to the code released by the
33;;;; Free Software Foundation under the name GUILE. If you copy
34;;;; code from other Free Software Foundation releases into a copy of
35;;;; GUILE, as the General Public License permits, the exception does
36;;;; not apply to the code that you add in this way. To avoid misleading
37;;;; anyone as to the status of such modified files, you must delete
38;;;; this exception notice from them.
39;;;;
40;;;; If you write modifications of your own for GUILE, it is your choice
41;;;; whether to permit this exception to apply to your modifications.
42;;;; If you do not wish that, delete this exception notice.
c771038b
TTN
43;;;;
44
45;;; Commentary:
46
47;; These procedures are exported:
48;; (adjoin e l)
49;; (union l1 l2)
50;; (intersection l1 l2)
51;; (set-difference l1 l2)
52;; (reduce-init p init l)
53;; (reduce p l)
54;; (some pred l . rest)
55;; (every pred l . rest)
56;; (notany pred . ls)
57;; (notevery pred . ls)
58;; (count-if pred l)
59;; (find-if pred l)
60;; (member-if pred l)
61;; (remove-if pred l)
62;; (remove-if-not pred l)
63;; (delete-if! pred l)
64;; (delete-if-not! pred l)
65;; (butlast lst n)
66;; (and? . args)
67;; (or? . args)
68;; (has-duplicates? lst)
69;; (pick p l)
70;; (pick-mappings p l)
71;; (uniq l)
72;;
73;; See docstrings for each procedure for more info. See also module
74;; `(srfi srfi-1)' for a complete list handling library.
75
76;;; Code:
6a4d3cfd 77\f
a6401ee0
JB
78(define-module (ice-9 common-list))
79
80;;"comlist.scm" Implementation of COMMON LISP list functions for Scheme
81; Copyright (C) 1991, 1993, 1995 Aubrey Jaffer.
82;
83;Permission to copy this software, to redistribute it, and to use it
84;for any purpose is granted, subject to the following restrictions and
85;understandings.
86;
87;1. Any copy made of this software must include this copyright notice
88;in full.
89;
90;2. I have made no warrantee or representation that the operation of
91;this software will be error-free, and I am under no obligation to
92;provide any services, by way of maintenance, update, or otherwise.
93;
94;3. In conjunction with products arising from the use of this
95;material, there shall be no use of my name in any advertising,
96;promotional, or sales literature without prior written consent in
97;each case.
98
c771038b
TTN
99(define-public (adjoin e l)
100 "Return list L, possibly with element E added if it is not already in L."
16693054 101 (if (memq e l) l (cons e l)))
a6401ee0
JB
102
103(define-public (union l1 l2)
c771038b
TTN
104 "Return a new list that is the union of L1 and L2.
105Elements that occur in both lists occur only once in
106the result list."
a6401ee0
JB
107 (cond ((null? l1) l2)
108 ((null? l2) l1)
109 (else (union (cdr l1) (adjoin (car l1) l2)))))
110
111(define-public (intersection l1 l2)
c771038b
TTN
112 "Return a new list that is the intersection of L1 and L2.
113Only elements that occur in both lists occur in the result list."
e5d2c2fa
DH
114 (if (null? l2) l2
115 (let loop ((l1 l1) (result '()))
116 (cond ((null? l1) (reverse! result))
117 ((memv (car l1) l2) (loop (cdr l1) (cons (car l1) result)))
118 (else (loop (cdr l1) result))))))
a6401ee0
JB
119
120(define-public (set-difference l1 l2)
16693054 121 "Return elements from list L1 that are not in list L2."
e5d2c2fa
DH
122 (let loop ((l1 l1) (result '()))
123 (cond ((null? l1) (reverse! result))
124 ((memv (car l1) l2) (loop (cdr l1) result))
125 (else (loop (cdr l1) (cons (car l1) result))))))
a6401ee0
JB
126
127(define-public (reduce-init p init l)
16693054 128 "Same as `reduce' except it implicitly inserts INIT at the start of L."
a6401ee0
JB
129 (if (null? l)
130 init
131 (reduce-init p (p init (car l)) (cdr l))))
132
133(define-public (reduce p l)
c771038b
TTN
134 "Combine all the elements of sequence L using a binary operation P.
135The combination is left-associative. For example, using +, one can
136add up all the elements. `reduce' allows you to apply a function which
16693054
GB
137accepts only two arguments to more than 2 objects. Functional
138programmers usually refer to this as foldl."
a6401ee0
JB
139 (cond ((null? l) l)
140 ((null? (cdr l)) (car l))
141 (else (reduce-init p (car l) (cdr l)))))
142
143(define-public (some pred l . rest)
16693054 144 "PRED is a boolean function of as many arguments as there are list
c771038b
TTN
145arguments to `some', i.e., L plus any optional arguments. PRED is
146applied to successive elements of the list arguments in order. As soon
147as one of these applications returns a true value, return that value.
148If no application returns a true value, return #f.
149All the lists should have the same length."
a6401ee0
JB
150 (cond ((null? rest)
151 (let mapf ((l l))
152 (and (not (null? l))
153 (or (pred (car l)) (mapf (cdr l))))))
154 (else (let mapf ((l l) (rest rest))
155 (and (not (null? l))
156 (or (apply pred (car l) (map car rest))
157 (mapf (cdr l) (map cdr rest))))))))
158
159(define-public (every pred l . rest)
16693054
GB
160 "Return #t iff every application of PRED to L, etc., returns #t.
161Analogous to `some' except it returns #t if every application of
162PRED is #t and #f otherwise."
a6401ee0
JB
163 (cond ((null? rest)
164 (let mapf ((l l))
165 (or (null? l)
166 (and (pred (car l)) (mapf (cdr l))))))
167 (else (let mapf ((l l) (rest rest))
168 (or (null? l)
169 (and (apply pred (car l) (map car rest))
170 (mapf (cdr l) (map cdr rest))))))))
171
c771038b 172(define-public (notany pred . ls)
16693054 173 "Return #t iff every application of PRED to L, etc., returns #f.
59dd1852
MV
174Analogous to some but returns #t if no application of PRED returns a
175true value or #f as soon as any one does."
16693054
GB
176 (not (apply some pred ls)))
177
c771038b 178(define-public (notevery pred . ls)
16693054
GB
179 "Return #t iff there is an application of PRED to L, etc., that returns #f.
180Analogous to some but returns #t as soon as an application of PRED returns #f,
181or #f otherwise."
182 (not (apply every pred ls)))
183
d69947f7 184(define-public (count-if pred l)
c771038b 185 "Return the number of elements in L for which (PRED element) returns true."
d69947f7
KN
186 (let loop ((n 0) (l l))
187 (cond ((null? l) n)
188 ((pred (car l)) (loop (+ n 1) (cdr l)))
189 (else (loop n (cdr l))))))
190
16693054 191(define-public (find-if pred l)
c771038b
TTN
192 "Search for the first element in L for which (PRED element) returns true.
193If found, return that element, otherwise return #f."
a6401ee0 194 (cond ((null? l) #f)
16693054
GB
195 ((pred (car l)) (car l))
196 (else (find-if pred (cdr l)))))
a6401ee0 197
16693054 198(define-public (member-if pred l)
abf94ef3 199 "Return the first sublist of L for whose car PRED is true."
a6401ee0 200 (cond ((null? l) #f)
16693054
GB
201 ((pred (car l)) l)
202 (else (member-if pred (cdr l)))))
a6401ee0 203
c771038b
TTN
204(define-public (remove-if pred l)
205 "Remove all elements from L where (PRED element) is true.
206Return everything that's left."
e5d2c2fa
DH
207 (let loop ((l l) (result '()))
208 (cond ((null? l) (reverse! result))
45cf8cd6 209 ((pred (car l)) (loop (cdr l) result))
e5d2c2fa 210 (else (loop (cdr l) (cons (car l) result))))))
a6401ee0 211
c771038b
TTN
212(define-public (remove-if-not pred l)
213 "Remove all elements from L where (PRED element) is #f.
214Return everything that's left."
e5d2c2fa
DH
215 (let loop ((l l) (result '()))
216 (cond ((null? l) (reverse! result))
45cf8cd6 217 ((not (pred (car l))) (loop (cdr l) result))
e5d2c2fa 218 (else (loop (cdr l) (cons (car l) result))))))
16693054 219
e5d2c2fa 220(define-public (delete-if! pred l)
16693054 221 "Destructive version of `remove-if'."
e5d2c2fa
DH
222 (let delete-if ((l l))
223 (cond ((null? l) '())
224 ((pred (car l)) (delete-if (cdr l)))
a6401ee0 225 (else
e5d2c2fa 226 (set-cdr! l (delete-if (cdr l)))
c771038b 227 l))))
a6401ee0 228
e5d2c2fa 229(define-public (delete-if-not! pred l)
16693054 230 "Destructive version of `remove-if-not'."
e5d2c2fa
DH
231 (let delete-if-not ((l l))
232 (cond ((null? l) '())
233 ((not (pred (car l))) (delete-if-not (cdr l)))
a6401ee0 234 (else
e5d2c2fa
DH
235 (set-cdr! l (delete-if-not (cdr l)))
236 l))))
a6401ee0
JB
237
238(define-public (butlast lst n)
16693054 239 "Return all but the last N elements of LST."
a6401ee0
JB
240 (letrec ((l (- (length lst) n))
241 (bl (lambda (lst n)
242 (cond ((null? lst) lst)
243 ((positive? n)
244 (cons (car lst) (bl (cdr lst) (+ -1 n))))
245 (else '())))))
246 (bl lst (if (negative? n)
247 (error "negative argument to butlast" n)
248 l))))
249
250(define-public (and? . args)
59dd1852 251 "Return #t iff all of ARGS are true."
a6401ee0
JB
252 (cond ((null? args) #t)
253 ((car args) (apply and? (cdr args)))
254 (else #f)))
255
256(define-public (or? . args)
59dd1852 257 "Return #t iff any of ARGS is true."
a6401ee0
JB
258 (cond ((null? args) #f)
259 ((car args) #t)
260 (else (apply or? (cdr args)))))
261
262(define-public (has-duplicates? lst)
16693054 263 "Return #t iff 2 members of LST are equal?, else #f."
a6401ee0
JB
264 (cond ((null? lst) #f)
265 ((member (car lst) (cdr lst)) #t)
266 (else (has-duplicates? (cdr lst)))))
267
a6401ee0 268(define-public (pick p l)
16693054
GB
269 "Apply P to each element of L, returning a list of elts
270for which P returns a non-#f value."
a6401ee0
JB
271 (let loop ((s '())
272 (l l))
273 (cond
274 ((null? l) s)
275 ((p (car l)) (loop (cons (car l) s) (cdr l)))
276 (else (loop s (cdr l))))))
277
a6401ee0 278(define-public (pick-mappings p l)
c771038b 279 "Apply P to each element of L, returning a list of the
16693054 280non-#f return values of P."
a6401ee0
JB
281 (let loop ((s '())
282 (l l))
283 (cond
284 ((null? l) s)
285 ((p (car l)) => (lambda (mapping) (loop (cons mapping s) (cdr l))))
286 (else (loop s (cdr l))))))
287
288(define-public (uniq l)
16693054 289 "Return a list containing elements of L, with duplicates removed."
23d91908
MV
290 (let loop ((acc '())
291 (l l))
292 (if (null? l)
293 (reverse! acc)
294 (loop (if (memq (car l) acc)
295 acc
296 (cons (car l) acc))
297 (cdr l)))))
c771038b
TTN
298
299;;; common-list.scm ends here