Install msysDTK-1.0.1
[msysgit.git] / share / guile / 1.6.0 / ice-9 / common-list.scm
blob29fa0e53658938fdcea438a8d886171d315f5231
1 ;;;; common-list.scm --- COMMON LISP list functions for Scheme
2 ;;;;
3 ;;;;    Copyright (C) 1995, 1996, 1997, 2001 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 ;;;; 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.
43 ;;;;
45 ;;; Commentary:
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)
73 ;; See docstrings for each procedure for more info.  See also module
74 ;; `(srfi srfi-1)' for a complete list handling library.
76 ;;; Code:
78 (define-module (ice-9 common-list)
79   :export (adjoin union intersection set-difference reduce-init reduce
80            some every notany notevery count-if find-if member-if remove-if
81            remove-if-not delete-if! delete-if-not! butlast and? or?
82            has-duplicates? pick pick-mappings uniq))
84 ;;"comlist.scm" Implementation of COMMON LISP list functions for Scheme
85 ; Copyright (C) 1991, 1993, 1995 Aubrey Jaffer.
87 ;Permission to copy this software, to redistribute it, and to use it
88 ;for any purpose is granted, subject to the following restrictions and
89 ;understandings.
91 ;1.  Any copy made of this software must include this copyright notice
92 ;in full.
94 ;2.  I have made no warrantee or representation that the operation of
95 ;this software will be error-free, and I am under no obligation to
96 ;provide any services, by way of maintenance, update, or otherwise.
98 ;3.  In conjunction with products arising from the use of this
99 ;material, there shall be no use of my name in any advertising,
100 ;promotional, or sales literature without prior written consent in
101 ;each case.
103 (define (adjoin e l)
104   "Return list L, possibly with element E added if it is not already in L."
105   (if (memq e l) l (cons e l)))
107 (define (union l1 l2)
108   "Return a new list that is the union of L1 and L2.
109 Elements that occur in both lists occur only once in
110 the result list."
111   (cond ((null? l1) l2)
112         ((null? l2) l1)
113         (else (union (cdr l1) (adjoin (car l1) l2)))))
115 (define (intersection l1 l2)
116   "Return a new list that is the intersection of L1 and L2.
117 Only elements that occur in both lists occur in the result list."
118   (if (null? l2) l2
119       (let loop ((l1 l1) (result '()))
120         (cond ((null? l1) (reverse! result))
121               ((memv (car l1) l2) (loop (cdr l1) (cons (car l1) result)))
122               (else (loop (cdr l1) result))))))
124 (define (set-difference l1 l2)
125   "Return elements from list L1 that are not in list L2."
126   (let loop ((l1 l1) (result '()))
127     (cond ((null? l1) (reverse! result))
128           ((memv (car l1) l2) (loop (cdr l1) result))
129           (else (loop (cdr l1) (cons (car l1) result))))))
131 (define (reduce-init p init l)
132   "Same as `reduce' except it implicitly inserts INIT at the start of L."
133   (if (null? l)
134       init
135       (reduce-init p (p init (car l)) (cdr l))))
137 (define (reduce p l)
138   "Combine all the elements of sequence L using a binary operation P.
139 The combination is left-associative.  For example, using +, one can
140 add up all the elements.  `reduce' allows you to apply a function which
141 accepts only two arguments to more than 2 objects.  Functional
142 programmers usually refer to this as foldl."
143   (cond ((null? l) l)
144         ((null? (cdr l)) (car l))
145         (else (reduce-init p (car l) (cdr l)))))
147 (define (some pred l . rest)
148   "PRED is a boolean function of as many arguments as there are list
149 arguments to `some', i.e., L plus any optional arguments.  PRED is
150 applied to successive elements of the list arguments in order.  As soon
151 as one of these applications returns a true value, return that value.
152 If no application returns a true value, return #f.
153 All the lists should have the same length."
154   (cond ((null? rest)
155          (let mapf ((l l))
156            (and (not (null? l))
157                 (or (pred (car l)) (mapf (cdr l))))))
158         (else (let mapf ((l l) (rest rest))
159                 (and (not (null? l))
160                      (or (apply pred (car l) (map car rest))
161                          (mapf (cdr l) (map cdr rest))))))))
163 (define (every pred l . rest)
164   "Return #t iff every application of PRED to L, etc., returns #t.
165 Analogous to `some' except it returns #t if every application of
166 PRED is #t and #f otherwise."
167   (cond ((null? rest)
168          (let mapf ((l l))
169            (or (null? l)
170                (and (pred (car l)) (mapf (cdr l))))))
171         (else (let mapf ((l l) (rest rest))
172                 (or (null? l)
173                     (and (apply pred (car l) (map car rest))
174                          (mapf (cdr l) (map cdr rest))))))))
176 (define (notany pred . ls)
177   "Return #t iff every application of PRED to L, etc., returns #f.
178 Analogous to some but returns #t if no application of PRED returns a
179 true value or #f as soon as any one does."
180   (not (apply some pred ls)))
182 (define (notevery pred . ls)
183   "Return #t iff there is an application of PRED to L, etc., that returns #f.
184 Analogous to some but returns #t as soon as an application of PRED returns #f,
185 or #f otherwise."
186   (not (apply every pred ls)))
188 (define (count-if pred l)
189   "Return the number of elements in L for which (PRED element) returns true."
190   (let loop ((n 0) (l l))
191     (cond ((null? l) n)
192           ((pred (car l)) (loop (+ n 1) (cdr l)))
193           (else (loop n (cdr l))))))
195 (define (find-if pred l)
196   "Search for the first element in L for which (PRED element) returns true.
197 If found, return that element, otherwise return #f."
198   (cond ((null? l) #f)
199         ((pred (car l)) (car l))
200         (else (find-if pred (cdr l)))))
202 (define (member-if pred l)
203   "Return the first sublist of L for whose car PRED is true."
204   (cond ((null? l) #f)
205         ((pred (car l)) l)
206         (else (member-if pred (cdr l)))))
208 (define (remove-if pred l)
209   "Remove all elements from L where (PRED element) is true.
210 Return everything that's left."
211   (let loop ((l l) (result '()))
212     (cond ((null? l) (reverse! result))
213           ((pred (car l)) (loop (cdr l) result))
214           (else (loop (cdr l) (cons (car l) result))))))
216 (define (remove-if-not pred l)
217   "Remove all elements from L where (PRED element) is #f.
218 Return everything that's left."
219   (let loop ((l l) (result '()))
220     (cond ((null? l) (reverse! result))
221           ((not (pred (car l))) (loop (cdr l) result))
222           (else (loop (cdr l) (cons (car l) result))))))
224 (define (delete-if! pred l)
225   "Destructive version of `remove-if'."
226   (let delete-if ((l l))
227     (cond ((null? l) '())
228           ((pred (car l)) (delete-if (cdr l)))
229           (else
230            (set-cdr! l (delete-if (cdr l)))
231            l))))
233 (define (delete-if-not! pred l)
234   "Destructive version of `remove-if-not'."
235   (let delete-if-not ((l l))
236     (cond ((null? l) '())
237           ((not (pred (car l))) (delete-if-not (cdr l)))
238           (else
239            (set-cdr! l (delete-if-not (cdr l)))
240            l))))
242 (define (butlast lst n)
243   "Return all but the last N elements of LST."
244   (letrec ((l (- (length lst) n))
245            (bl (lambda (lst n)
246                  (cond ((null? lst) lst)
247                        ((positive? n)
248                         (cons (car lst) (bl (cdr lst) (+ -1 n))))
249                        (else '())))))
250     (bl lst (if (negative? n)
251                 (error "negative argument to butlast" n)
252                 l))))
254 (define (and? . args)
255   "Return #t iff all of ARGS are true."
256   (cond ((null? args) #t)
257         ((car args) (apply and? (cdr args)))
258         (else #f)))
260 (define (or? . args)
261   "Return #t iff any of ARGS is true."
262   (cond ((null? args) #f)
263         ((car args) #t)
264         (else (apply or? (cdr args)))))
266 (define (has-duplicates? lst)
267   "Return #t iff 2 members of LST are equal?, else #f."
268   (cond ((null? lst) #f)
269         ((member (car lst) (cdr lst)) #t)
270         (else (has-duplicates? (cdr lst)))))
272 (define (pick p l)
273   "Apply P to each element of L, returning a list of elts
274 for which P returns a non-#f value."
275   (let loop ((s '())
276              (l l))
277     (cond
278      ((null? l)         s)
279      ((p (car l))       (loop (cons (car l) s) (cdr l)))
280      (else              (loop s (cdr l))))))
282 (define (pick-mappings p l)
283   "Apply P to each element of L, returning a list of the
284 non-#f return values of P."
285   (let loop ((s '())
286              (l l))
287     (cond
288      ((null? l)         s)
289      ((p (car l)) =>    (lambda (mapping) (loop (cons mapping s) (cdr l))))
290      (else              (loop s (cdr l))))))
292 (define (uniq l)
293   "Return a list containing elements of L, with duplicates removed."
294   (let loop ((acc '())
295              (l l))
296     (if (null? l)
297         (reverse! acc)
298         (loop (if (memq (car l) acc)
299                   acc
300                   (cons (car l) acc))
301               (cdr l)))))
303 ;;; common-list.scm ends here