File: intersection.scm

package info (click to toggle)
scheme9 2025.08.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,080 kB
  • sloc: lisp: 16,752; ansic: 11,869; sh: 806; makefile: 237; sed: 6
file content (25 lines) | stat: -rw-r--r-- 705 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
; Scheme 9 from Empty Space, Function Library
; By Nils M Holm, 2009
; Placed in the Public Domain
;
; (intersection list ...)  ==>  list
;
; Compute the intersection of a number of sets.
;
; Example:   (intersection '(v w x) '(w x y) '(x y z))  ==>  (x)

(define (intersection . a*)
  (letrec
    ((intersection3 (lambda (a b r)
      (cond ((null? a)
              (reverse! r))
            ((member (car a) b)
              (intersection3 (cdr a) b (cons (car a) r)))
            (else
              (intersection3 (cdr a) b r))))))
    (if (null? a*)
        a*
        (fold-left (lambda (a b)
                     (intersection3 a b '()))
                   (car a*)
                   (cdr a*)))))