-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset-ordered-list.rkt
37 lines (31 loc) · 1.23 KB
/
set-ordered-list.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
#lang racket
(provide union-set intersection-set element-of-set? adjoin-set make-set)
(require "accumulate.rkt")
(define (element-of-set? x set)
(cond ((null? set) #f)
((= x (car set)) #t)
((< x (car set)) #f)
(else (element-of-set? x (cdr set)))))
(define (adjoin-set x set)
(cond ((null? set) (list x))
((= (car set) x) set)
((< (car set) x) (cons (car set) (adjoin-set x (cdr set))))
(else (cons x set))))
(define (intersection-set set1 set2)
(if (or (null? set1) (null? set2))
'()
(let ((x1 (car set1))
(x2 (car set2)))
(cond ((= x1 x2) (cons x1 (intersection-set (cdr set1) (cdr set2))))
((< x1 x2) (intersection-set (cdr set1) set2))
(else (intersection-set set1 (cdr set2)))))))
(define (union-set set1 set2)
(cond ((null? set1) set2)
((null? set2) set1)
(else (let ((x1 (car set1))
(x2 (car set2)))
(cond ((= x1 x2) (cons x1 (union-set (cdr set1) (cdr set2))))
((< x1 x2) (cons x1 (union-set (cdr set1) set2)))
(else (cons x1 (union-set set1 (cdr set2)))))))))
(define (make-set . elements)
(accumulate adjoin-set '() elements))