t3x.org / nss / intersection.html

(Nils' Scheme Snippets)

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

(intersection . list) => list

 
Purpose
Compute the intersection of a number of sets.
 
Arguments
a* sets
 
Dependencies
fold-left
Example
(intersection '(v w x) '(w x y) '(x y z)) => (x)
(load "fold-left.scm")

(define (intersection . a*)
  (letrec
    ((intersection2
       (lambda (a b)
         (cond
           ((null? a) '())
           ((member (car a) b)
             (cons (car a) (intersection2 (cdr a) b)))
           (else (intersection2 (cdr a) b))))))
    (cond ((null? a*) '())
      (else (fold-left intersection2 (car a*) (cdr a*))))))

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