Commit | Line | Data |
---|---|---|
6a4d3cfd JB |
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 | |
a6401ee0 JB |
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 | ||
16693054 GB |
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))) | |
a6401ee0 JB |
45 | |
46 | (define-public (union l1 l2) | |
16693054 GB |
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." | |
a6401ee0 JB |
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) | |
16693054 GB |
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." | |
e5d2c2fa DH |
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)))))) | |
a6401ee0 JB |
62 | |
63 | (define-public (set-difference l1 l2) | |
16693054 | 64 | "Return elements from list L1 that are not in list L2." |
e5d2c2fa DH |
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)))))) | |
a6401ee0 JB |
69 | |
70 | (define-public (reduce-init p init l) | |
16693054 | 71 | "Same as `reduce' except it implicitly inserts INIT at the start of L." |
a6401ee0 JB |
72 | (if (null? l) |
73 | init | |
74 | (reduce-init p (p init (car l)) (cdr l)))) | |
75 | ||
76 | (define-public (reduce p l) | |
16693054 GB |
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." | |
a6401ee0 JB |
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) | |
16693054 GB |
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 | |
59dd1852 MV |
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." | |
a6401ee0 JB |
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) | |
16693054 GB |
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." | |
a6401ee0 JB |
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 | ||
16693054 GB |
115 | (define-public (notany pred . ls) |
116 | "Return #t iff every application of PRED to L, etc., returns #f. | |
59dd1852 MV |
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." | |
16693054 GB |
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 | ||
d69947f7 KN |
127 | (define-public (count-if pred l) |
128 | "Returns the number of elements in L such that (PRED element) | |
129 | returns true." | |
130 | (let loop ((n 0) (l l)) | |
131 | (cond ((null? l) n) | |
132 | ((pred (car l)) (loop (+ n 1) (cdr l))) | |
133 | (else (loop n (cdr l)))))) | |
134 | ||
16693054 GB |
135 | (define-public (find-if pred l) |
136 | "Searches for the first element in L such that (PRED element) | |
59dd1852 | 137 | returns true. If it finds any such element in L, element is |
16693054 | 138 | returned. Otherwise, #f is returned." |
a6401ee0 | 139 | (cond ((null? l) #f) |
16693054 GB |
140 | ((pred (car l)) (car l)) |
141 | (else (find-if pred (cdr l))))) | |
a6401ee0 | 142 | |
16693054 | 143 | (define-public (member-if pred l) |
59dd1852 MV |
144 | "Returns L if (T element) is true for any element in L. Returns #f |
145 | if PRED does not apply to any element in L." | |
a6401ee0 | 146 | (cond ((null? l) #f) |
16693054 GB |
147 | ((pred (car l)) l) |
148 | (else (member-if pred (cdr l))))) | |
a6401ee0 | 149 | |
e5d2c2fa DH |
150 | (define-public (remove-if pred? l) |
151 | "Removes all elements from L where (PRED? element) is true. | |
16693054 | 152 | Returns everything that's left." |
e5d2c2fa DH |
153 | (let loop ((l l) (result '())) |
154 | (cond ((null? l) (reverse! result)) | |
155 | ((pred? (car l)) (loop (cdr l) result)) | |
156 | (else (loop (cdr l) (cons (car l) result)))))) | |
a6401ee0 | 157 | |
e5d2c2fa DH |
158 | (define-public (remove-if-not pred? l) |
159 | "Removes all elements from L where (PRED? element) is #f. | |
16693054 | 160 | Returns everything that's left." |
e5d2c2fa DH |
161 | (let loop ((l l) (result '())) |
162 | (cond ((null? l) (reverse! result)) | |
163 | ((not (pred? (car l))) (loop (cdr l) result)) | |
164 | (else (loop (cdr l) (cons (car l) result)))))) | |
16693054 | 165 | |
e5d2c2fa | 166 | (define-public (delete-if! pred l) |
16693054 | 167 | "Destructive version of `remove-if'." |
e5d2c2fa DH |
168 | (let delete-if ((l l)) |
169 | (cond ((null? l) '()) | |
170 | ((pred (car l)) (delete-if (cdr l))) | |
a6401ee0 | 171 | (else |
e5d2c2fa DH |
172 | (set-cdr! l (delete-if (cdr l))) |
173 | l)))) | |
a6401ee0 | 174 | |
e5d2c2fa | 175 | (define-public (delete-if-not! pred l) |
16693054 | 176 | "Destructive version of `remove-if-not'." |
e5d2c2fa DH |
177 | (let delete-if-not ((l l)) |
178 | (cond ((null? l) '()) | |
179 | ((not (pred (car l))) (delete-if-not (cdr l))) | |
a6401ee0 | 180 | (else |
e5d2c2fa DH |
181 | (set-cdr! l (delete-if-not (cdr l))) |
182 | l)))) | |
a6401ee0 JB |
183 | |
184 | (define-public (butlast lst n) | |
16693054 | 185 | "Return all but the last N elements of LST." |
a6401ee0 JB |
186 | (letrec ((l (- (length lst) n)) |
187 | (bl (lambda (lst n) | |
188 | (cond ((null? lst) lst) | |
189 | ((positive? n) | |
190 | (cons (car lst) (bl (cdr lst) (+ -1 n)))) | |
191 | (else '()))))) | |
192 | (bl lst (if (negative? n) | |
193 | (error "negative argument to butlast" n) | |
194 | l)))) | |
195 | ||
196 | (define-public (and? . args) | |
59dd1852 | 197 | "Return #t iff all of ARGS are true." |
a6401ee0 JB |
198 | (cond ((null? args) #t) |
199 | ((car args) (apply and? (cdr args))) | |
200 | (else #f))) | |
201 | ||
202 | (define-public (or? . args) | |
59dd1852 | 203 | "Return #t iff any of ARGS is true." |
a6401ee0 JB |
204 | (cond ((null? args) #f) |
205 | ((car args) #t) | |
206 | (else (apply or? (cdr args))))) | |
207 | ||
208 | (define-public (has-duplicates? lst) | |
16693054 | 209 | "Return #t iff 2 members of LST are equal?, else #f." |
a6401ee0 JB |
210 | (cond ((null? lst) #f) |
211 | ((member (car lst) (cdr lst)) #t) | |
212 | (else (has-duplicates? (cdr lst))))) | |
213 | ||
a6401ee0 | 214 | (define-public (pick p l) |
16693054 GB |
215 | "Apply P to each element of L, returning a list of elts |
216 | for which P returns a non-#f value." | |
a6401ee0 JB |
217 | (let loop ((s '()) |
218 | (l l)) | |
219 | (cond | |
220 | ((null? l) s) | |
221 | ((p (car l)) (loop (cons (car l) s) (cdr l))) | |
222 | (else (loop s (cdr l)))))) | |
223 | ||
a6401ee0 | 224 | (define-public (pick-mappings p l) |
16693054 GB |
225 | "Apply P to each element of L, returning a list of the |
226 | non-#f return values of P." | |
a6401ee0 JB |
227 | (let loop ((s '()) |
228 | (l l)) | |
229 | (cond | |
230 | ((null? l) s) | |
231 | ((p (car l)) => (lambda (mapping) (loop (cons mapping s) (cdr l)))) | |
232 | (else (loop s (cdr l)))))) | |
233 | ||
234 | (define-public (uniq l) | |
16693054 | 235 | "Return a list containing elements of L, with duplicates removed." |
23d91908 MV |
236 | (let loop ((acc '()) |
237 | (l l)) | |
238 | (if (null? l) | |
239 | (reverse! acc) | |
240 | (loop (if (memq (car l) acc) | |
241 | acc | |
242 | (cons (car l) acc)) | |
243 | (cdr l))))) |