-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.rkt
53 lines (40 loc) · 1.42 KB
/
set.rkt
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#lang racket
(require "member.rkt" "remove-member.rkt" "equal.rkt")
(provide set? makeset subset? eqset? intersect? intersection union diff)
(define (set? things)
(cond
[(null? things) #t]
[else (and (not (member? (car things) (cdr things))) (set? (cdr things)))]))
(define (makeset things)
(cond
[(null? things) (list)]
[else (cons (car things) (makeset (multi-remove-member equal? (car things) (cdr things))))]))
(define (subset? setx sety)
(cond
[(null? setx) #t]
[else (and (member? (car setx) sety) (subset? (cdr setx) sety))]))
(define (eqset? setx sety)
(and (subset? setx sety) (subset? sety setx)))
(define (intersect? setx sety)
(cond
[(null? setx) #f]
[else (or (member? (car setx) sety) (intersect? (cdr setx) sety))]))
(define (intersection setx sety)
(cond
[(null? setx) (list)]
[(member? (car setx) sety) (cons (car setx) (intersection (cdr setx) sety))]
[else (intersection (cdr setx) sety)]))
(define (union setx sety)
(cond
[(null? setx) sety]
[(member? (car setx) sety) (union (cdr setx) sety)]
[else (cons (car setx) (union (cdr setx) sety))]))
(define (diff setx sety)
(cond
[(null? setx) (list)]
[(member? (car setx) sety) (diff (cdr setx) sety)]
[else (cons (car setx) (diff (cdr setx) sety))]))
(define (intersect-all sets)
(cond
[(null? (cdr sets)) (car sets)]
[else (intersection (car sets) (intersect-all (cdr sets)))]))