-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsets.rkt
executable file
·90 lines (63 loc) · 2.16 KB
/
sets.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname sets) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
;; sets
(require rackunit)
(require "extras.rkt")
(provide my-member? subset? set-equal?
set-cons set-union set-minus set-diff)
;; A SetOf<X> is a ListOf<X> WITH NO DUPLICATES
;; note: empty is a SetOf<X>
;; my-member? : X SetOf<X> -> Boolean
;; strategy: HO Function Combination
(define (my-member? x set1)
(ormap
(lambda (z) (equal? x z))
set1))
(check-true (my-member? 3 (list 1 3 5)))
(check-false (my-member? 4 (list 1 3 5)))
;; subset? : SetOf<X> SetOf<X> -> Boolean
;; strategy: HO Function Combination
(define (subset? set1 set2)
(andmap
(lambda (x) (my-member? x set2))
set1))
(check-true (subset? (list 1 3 5) (list 1 3 2 4 5 8)))
(check-false (subset? (list 1 3 5) (list 1 3 8)))
;; set-equal? : SetOf<X> SetOf<X> -> Boolean
(define (set-equal? set1 set2)
(and
(subset? set1 set2)
(subset? set2 set1)))
(check-true (set-equal? (list 1 3 5) (list 3 5 1)))
(check-false (set-equal? (list 1 3 5) (list 1 3 4 5)))
(check-false (set-equal? (list 1 3 5) (list 1 3 5 7)))
(check-false (set-equal? (list 1 3 5 7) (list 1 3 5)))
;; these should have tests:
;; set-cons : X SetOf<X> -> SetOf<X>
(define (set-cons x set1)
(if (my-member? x set1)
set1
(cons x set1)))
;; set-union : SetOf<X> SetOf<X> -> SetOf<X>
(define (set-union set1 set2)
(foldr
set-cons
set2
set1))
;; this starts with set2 and adds the elements of set1 one at a time.
;; This makes set-union work like append.
(check-equal?
(set-union '(a b c) '(c b d))
'(a c b d))
;; set-minus : SetOf<X> X -> SetOf<X>
(define (set-minus set1 x)
(filter
(lambda (elt) (not (equal? x elt)))
set1))
;; set-diff : SetOf<X> SetOf<X> -> SetOf<X>
;; return all elements of set1 that are NOT elements of set2
(define (set-diff set1 set2)
(filter
(lambda (elt) (not (my-member? elt set2)))
set1))