1 ;;;; common-list.scm --- COMMON LISP list functions for Scheme
3 ;;;; Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
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.
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.
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
21 (define-module (ice-9 common-list))
23 ;;"comlist.scm" Implementation of COMMON LISP list functions for Scheme
24 ; Copyright (C) 1991, 1993, 1995 Aubrey Jaffer.
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
30 ;1. Any copy made of this software must include this copyright notice
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.
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
42 (define-public (adjoin e l) (if (memq e l) l (cons e l)))
44 (define-public (union l1 l2)
47 (else (union (cdr l1) (adjoin (car l1) l2)))))
49 (define-public (intersection l1 l2)
52 ((memv (car l1) l2) (cons (car l1) (intersection (cdr l1) l2)))
53 (else (intersection (cdr l1) l2))))
55 (define-public (set-difference l1 l2)
57 ((memv (car l1) l2) (set-difference (cdr l1) l2))
58 (else (cons (car l1) (set-difference (cdr l1) l2)))))
60 (define-public (reduce-init p init l)
63 (reduce-init p (p init (car l)) (cdr l))))
65 (define-public (reduce p l)
67 ((null? (cdr l)) (car l))
68 (else (reduce-init p (car l) (cdr l)))))
70 (define-public (some pred l . rest)
74 (or (pred (car l)) (mapf (cdr l))))))
75 (else (let mapf ((l l) (rest rest))
77 (or (apply pred (car l) (map car rest))
78 (mapf (cdr l) (map cdr rest))))))))
80 (define-public (every pred l . rest)
84 (and (pred (car l)) (mapf (cdr l))))))
85 (else (let mapf ((l l) (rest rest))
87 (and (apply pred (car l) (map car rest))
88 (mapf (cdr l) (map cdr rest))))))))
90 (define-public (notany pred . ls) (not (apply some pred ls)))
92 (define-public (notevery pred . ls) (not (apply every pred ls)))
94 (define-public (find-if t l)
97 (else (find-if t (cdr l)))))
99 (define-public (member-if t l)
102 (else (member-if t (cdr l)))))
104 (define-public (remove-if p l)
105 (cond ((null? l) '())
106 ((p (car l)) (remove-if p (cdr l)))
107 (else (cons (car l) (remove-if p (cdr l))))))
109 (define-public (delete-if! pred list)
110 (let delete-if ((list list))
111 (cond ((null? list) '())
112 ((pred (car list)) (delete-if (cdr list)))
114 (set-cdr! list (delete-if (cdr list)))
117 (define-public (delete-if-not! pred list)
118 (let delete-if ((list list))
119 (cond ((null? list) '())
120 ((not (pred (car list))) (delete-if (cdr list)))
122 (set-cdr! list (delete-if (cdr list)))
125 (define-public (butlast lst n)
126 (letrec ((l (- (length lst) n))
128 (cond ((null? lst) lst)
130 (cons (car lst) (bl (cdr lst) (+ -1 n))))
132 (bl lst (if (negative? n)
133 (error "negative argument to butlast" n)
136 (define-public (and? . args)
137 (cond ((null? args) #t)
138 ((car args) (apply and? (cdr args)))
141 (define-public (or? . args)
142 (cond ((null? args) #f)
144 (else (apply or? (cdr args)))))
146 (define-public (has-duplicates? lst)
147 (cond ((null? lst) #f)
148 ((member (car lst) (cdr lst)) #t)
149 (else (has-duplicates? (cdr lst)))))
151 (define-public (list* x . y)
155 (cons (car x) (list*1 (cdr x)))))
158 (cons x (list*1 y))))
161 ;; Apply P to each element of L, returning a list of elts
162 ;; for which P returns a non-#f value.
164 (define-public (pick p l)
169 ((p (car l)) (loop (cons (car l) s) (cdr l)))
170 (else (loop s (cdr l))))))
173 ;; Apply P to each element of L, returning a list of the
174 ;; non-#f return values of P.
176 (define-public (pick-mappings p l)
181 ((p (car l)) => (lambda (mapping) (loop (cons mapping s) (cdr l))))
182 (else (loop s (cdr l))))))
184 (define-public (uniq l)
187 (let ((u (uniq (cdr l))))