t3x.org / nss / for-all.html

(Nils' Scheme Snippets)

 
Paren matching: ON  |  Category: math  |  Overview  |  Scheme Books  |  License
 

(for-all proc . list) => form

 
Purpose
Test whether all members of a sequence of lists have a given property. The property is expressed using an n-ary predicate p. P is applied to a list consisting of the first member of each given list. If p returns truth, it is applied to a list consisting of the second member of each given list, etc. If p returns falsity for any set of members, for-all returns #f. If only one set of members is left to check, for-all returns the value of p applied to this last set.
 
Arguments
proc predicate
a* lists
 
Example
(for-all < '(1 7) '(2 8) '(3 9)) => #t
; because (< 1 2 3) and (< 7 8 9)
(define (for-all p . a*)
  (letrec
    ((carof
       (lambda (a)
         (map car a)))
     (cdrof
       (lambda (a)
         (map cdr a)))
     (forall*
       (lambda (a*)
         (cond
           ((null? (car a*)) #t)
           ((null? (cdar a*))
             (apply p (carof a*)))
           (else (and (apply p (carof a*))
                      (forall* (cdrof a*))))))))
    (forall* a*)))

Copyright (C) 2007 Nils M Holm <nmh @ t3x . org>