* Made a couple of functions (not all yet) tail recursive.
[bpt/guile.git] / ice-9 / common-list.scm
1 ;;;; common-list.scm --- COMMON LISP list functions for Scheme
2 ;;;;
3 ;;;; Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
4 ;;;;
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.
9 ;;;;
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.
14 ;;;;
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
19 ;;;;
20 \f
21 (define-module (ice-9 common-list))
22
23 ;;"comlist.scm" Implementation of COMMON LISP list functions for Scheme
24 ; Copyright (C) 1991, 1993, 1995 Aubrey Jaffer.
25 ;
26 ;Permission to copy this software, to redistribute it, and to use it
27 ;for any purpose is granted, subject to the following restrictions and
28 ;understandings.
29 ;
30 ;1. Any copy made of this software must include this copyright notice
31 ;in full.
32 ;
33 ;2. I have made no warrantee or representation that the operation of
34 ;this software will be error-free, and I am under no obligation to
35 ;provide any services, by way of maintenance, update, or otherwise.
36 ;
37 ;3. In conjunction with products arising from the use of this
38 ;material, there shall be no use of my name in any advertising,
39 ;promotional, or sales literature without prior written consent in
40 ;each case.
41
42 (define-public (adjoin e l)
43 "Returns list L, possibly with element E added if it is not already in L."
44 (if (memq e l) l (cons e l)))
45
46 (define-public (union l1 l2)
47 "Returns a new list that is the union of L1 and L2.
48 Elements that occur in both lists will occur only once
49 in the result list."
50 (cond ((null? l1) l2)
51 ((null? l2) l1)
52 (else (union (cdr l1) (adjoin (car l1) l2)))))
53
54 (define-public (intersection l1 l2)
55 "Returns a new list that is the intersection of L1 and L2.
56 Only elements that occur in both lists will occur in the result list."
57 (if (null? l2) l2
58 (let loop ((l1 l1) (result '()))
59 (cond ((null? l1) (reverse! result))
60 ((memv (car l1) l2) (loop (cdr l1) (cons (car l1) result)))
61 (else (loop (cdr l1) result))))))
62
63 (define-public (set-difference l1 l2)
64 "Return elements from list L1 that are not in list L2."
65 (let loop ((l1 l1) (result '()))
66 (cond ((null? l1) (reverse! result))
67 ((memv (car l1) l2) (loop (cdr l1) result))
68 (else (loop (cdr l1) (cons (car l1) result))))))
69
70 (define-public (reduce-init p init l)
71 "Same as `reduce' except it implicitly inserts INIT at the start of L."
72 (if (null? l)
73 init
74 (reduce-init p (p init (car l)) (cdr l))))
75
76 (define-public (reduce p l)
77 "Combines all the elements of sequence L using a binary operation P.
78 The combination is left-associative. For example, using +, one can
79 add up all the elements. `reduce' allows you to apply a function which
80 accepts only two arguments to more than 2 objects. Functional
81 programmers usually refer to this as foldl."
82 (cond ((null? l) l)
83 ((null? (cdr l)) (car l))
84 (else (reduce-init p (car l) (cdr l)))))
85
86 (define-public (some pred l . rest)
87 "PRED is a boolean function of as many arguments as there are list
88 arguments to `some'. I.e., L plus any optional arguments. PRED is
89 applied to successive elements of the list arguments in order. As soon
90 as one of these applications returns a true value, `some' terminates
91 and returns that value. If no application returns a true value,
92 `some' returns #f. All the lists should have the same length."
93 (cond ((null? rest)
94 (let mapf ((l l))
95 (and (not (null? l))
96 (or (pred (car l)) (mapf (cdr l))))))
97 (else (let mapf ((l l) (rest rest))
98 (and (not (null? l))
99 (or (apply pred (car l) (map car rest))
100 (mapf (cdr l) (map cdr rest))))))))
101
102 (define-public (every pred l . rest)
103 "Return #t iff every application of PRED to L, etc., returns #t.
104 Analogous to `some' except it returns #t if every application of
105 PRED is #t and #f otherwise."
106 (cond ((null? rest)
107 (let mapf ((l l))
108 (or (null? l)
109 (and (pred (car l)) (mapf (cdr l))))))
110 (else (let mapf ((l l) (rest rest))
111 (or (null? l)
112 (and (apply pred (car l) (map car rest))
113 (mapf (cdr l) (map cdr rest))))))))
114
115 (define-public (notany pred . ls)
116 "Return #t iff every application of PRED to L, etc., returns #f.
117 Analogous to some but returns #t if no application of PRED returns a
118 true value or #f as soon as any one does."
119 (not (apply some pred ls)))
120
121 (define-public (notevery pred . ls)
122 "Return #t iff there is an application of PRED to L, etc., that returns #f.
123 Analogous to some but returns #t as soon as an application of PRED returns #f,
124 or #f otherwise."
125 (not (apply every pred ls)))
126
127 (define-public (find-if pred l)
128 "Searches for the first element in L such that (PRED element)
129 returns true. If it finds any such element in L, element is
130 returned. Otherwise, #f is returned."
131 (cond ((null? l) #f)
132 ((pred (car l)) (car l))
133 (else (find-if pred (cdr l)))))
134
135 (define-public (member-if pred l)
136 "Returns L if (T element) is true for any element in L. Returns #f
137 if PRED does not apply to any element in L."
138 (cond ((null? l) #f)
139 ((pred (car l)) l)
140 (else (member-if pred (cdr l)))))
141
142 (define-public (remove-if pred? l)
143 "Removes all elements from L where (PRED? element) is true.
144 Returns everything that's left."
145 (let loop ((l l) (result '()))
146 (cond ((null? l) (reverse! result))
147 ((pred? (car l)) (loop (cdr l) result))
148 (else (loop (cdr l) (cons (car l) result))))))
149
150 (define-public (remove-if-not pred? l)
151 "Removes all elements from L where (PRED? element) is #f.
152 Returns everything that's left."
153 (let loop ((l l) (result '()))
154 (cond ((null? l) (reverse! result))
155 ((not (pred? (car l))) (loop (cdr l) result))
156 (else (loop (cdr l) (cons (car l) result))))))
157
158 (define-public (delete-if! pred l)
159 "Destructive version of `remove-if'."
160 (let delete-if ((l l))
161 (cond ((null? l) '())
162 ((pred (car l)) (delete-if (cdr l)))
163 (else
164 (set-cdr! l (delete-if (cdr l)))
165 l))))
166
167 (define-public (delete-if-not! pred l)
168 "Destructive version of `remove-if-not'."
169 (let delete-if-not ((l l))
170 (cond ((null? l) '())
171 ((not (pred (car l))) (delete-if-not (cdr l)))
172 (else
173 (set-cdr! l (delete-if-not (cdr l)))
174 l))))
175
176 (define-public (butlast lst n)
177 "Return all but the last N elements of LST."
178 (letrec ((l (- (length lst) n))
179 (bl (lambda (lst n)
180 (cond ((null? lst) lst)
181 ((positive? n)
182 (cons (car lst) (bl (cdr lst) (+ -1 n))))
183 (else '())))))
184 (bl lst (if (negative? n)
185 (error "negative argument to butlast" n)
186 l))))
187
188 (define-public (and? . args)
189 "Return #t iff all of ARGS are true."
190 (cond ((null? args) #t)
191 ((car args) (apply and? (cdr args)))
192 (else #f)))
193
194 (define-public (or? . args)
195 "Return #t iff any of ARGS is true."
196 (cond ((null? args) #f)
197 ((car args) #t)
198 (else (apply or? (cdr args)))))
199
200 (define-public (has-duplicates? lst)
201 "Return #t iff 2 members of LST are equal?, else #f."
202 (cond ((null? lst) #f)
203 ((member (car lst) (cdr lst)) #t)
204 (else (has-duplicates? (cdr lst)))))
205
206 (define-public (pick p l)
207 "Apply P to each element of L, returning a list of elts
208 for which P returns a non-#f value."
209 (let loop ((s '())
210 (l l))
211 (cond
212 ((null? l) s)
213 ((p (car l)) (loop (cons (car l) s) (cdr l)))
214 (else (loop s (cdr l))))))
215
216 (define-public (pick-mappings p l)
217 "Apply P to each element of L, returning a list of the
218 non-#f return values of P."
219 (let loop ((s '())
220 (l l))
221 (cond
222 ((null? l) s)
223 ((p (car l)) => (lambda (mapping) (loop (cons mapping s) (cdr l))))
224 (else (loop s (cdr l))))))
225
226 (define-public (uniq l)
227 "Return a list containing elements of L, with duplicates removed."
228 (if (null? l)
229 '()
230 (let ((u (uniq (cdr l))))
231 (if (memq (car l) u)
232 u
233 (cons (car l) u)))))
234