1 ;;;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3 ;;;; This program is free software; you can redistribute it and/or modify
4 ;;;; it under the terms of the GNU General Public License as published by
5 ;;;; the Free Software Foundation; either version 2, or (at your option)
6 ;;;; any later version.
8 ;;;; This program is distributed in the hope that it will be useful,
9 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ;;;; GNU General Public License for more details.
13 ;;;; You should have received a copy of the GNU General Public License
14 ;;;; along with this software; see the file COPYING. If not, write to
15 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 ;;;; Boston, MA 02111-1307 USA
20 (define-module (oop goops util)
24 (export any every filter
25 mapappend find-duplicate top-level-env top-level-env?
26 map* for-each* length* improper->proper
33 (define (any pred lst . rest)
34 (if (null? rest) ;fast path
35 (and (not (null? lst))
36 (let loop ((head (car lst)) (tail (cdr lst)))
40 (loop (car tail) (cdr tail))))))
41 (let ((lsts (cons lst rest)))
42 (and (not (any null? lsts))
43 (let loop ((heads (map car lsts)) (tails (map cdr lsts)))
46 (or (apply pred heads)
47 (loop (map car tails) (map cdr tails)))))))))
49 (define (every pred lst . rest)
50 (if (null? rest) ;fast path
52 (let loop ((head (car lst)) (tail (cdr lst)))
56 (loop (car tail) (cdr tail))))))
57 (let ((lsts (cons lst rest)))
59 (let loop ((heads (map car lsts)) (tails (map cdr lsts)))
62 (and (apply pred heads)
63 (loop (map car tails) (map cdr tails)))))))))
65 (define (filter test? list)
66 (cond ((null? list) '())
67 ((test? (car list)) (cons (car list) (filter test? (cdr list))))
68 (else (filter test? (cdr list)))))
70 (define (mapappend func . args)
73 (append (apply func (map car args))
74 (apply mapappend func (map cdr args)))))
76 (define (find-duplicate l) ; find a duplicate in a list; #f otherwise
79 ((memv (car l) (cdr l)) (car l))
80 (else (find-duplicate (cdr l)))))
82 (define (top-level-env)
83 (if *top-level-lookup-closure*
84 (list *top-level-lookup-closure*)
87 (define (top-level-env? env)
89 (procedure? (car env))))
91 (define (map* fn . l) ; A map which accepts dotted lists (arg lists
92 (cond ; must be "isomorph"
94 ((pair? (car l)) (cons (apply fn (map car l))
95 (apply map* fn (map cdr l))))
98 (define (for-each* fn . l) ; A for-each which accepts dotted lists (arg lists
99 (cond ; must be "isomorph"
100 ((null? (car l)) '())
101 ((pair? (car l)) (apply fn (map car l)) (apply for-each* fn (map cdr l)))
102 (else (apply fn l))))
107 ((not (pair? ls)) n)))
109 (define (improper->proper ls)
111 (cons (car ls) (improper->proper (cdr ls)))