1 #lang racket
2
3 ;;;;;;;;;;;;;;;;;;;;
2.59
4 (define (element-of-
set? x
set)
5 (cond ((
null?
set)
false)
6 ((equal? x (car
set))
true)
7 (
else (element-of-
set? x (cdr
set)))))
8
9 (define (adjoin-
set x
set)
10 (
if (element-of-
set? x
set)
11 set
12 (cons x
set)))
13
14 (define (intersection-
set set1 set2)
15 (cond ((or (
null? set1) (
null? set2))
'())
16 ((element-of-
set?
(car set1) set2)
17 (cons (car set1)
18 (intersection-
set (cdr set1) set2)))
19 (
else (intersection-
set (cdr set1) set2))))
20
21 (define (union-
set set1 set2)
22 (cond ((
null?
set1) set2)
23 ((
null?
set2) set1)
24 ((element-of-
set?
(car set1) set2)
25 (union-
set (cdr set1) set2))
26 (
else (cons (car set1)
27 (union-
set (cdr set1) set2)))))
28
29 ;;;;;;;;;;;;;;;;;;;
2.60
30 (define (
new-element-of-
set? x
set)
31 (cond ((
null?
set)
false)
32 ((equal? x (car
set))
true)
33 (
else (element-of-
set? x (cdr
set)))))
34
35 (define (
new-adjion-
set x
set)
36 (cons x
set))
37
38 (define (
new-union-
set set1 set2)
39 (append set1 set2))
40
41 (define (
new-intersection-
set set1 set2)
42 (cond ((or (
null? set1) (
null? set2))
'())
43 ((element-of-
set?
(car set1) set2)
44 (cons (car set1)
45 (intersection-
set (cdr set1) set2)))
46 (
else (intersection-
set (cdr set1) set2))))
47
48 ;;;;;;;;;;;;;;;;;test
49 (union-
set '(1 2 3 4) '(
0 3 5 1))
50 (
new-union-
set '(1 2 3 2 4) '(
3 4 3 2 1 5))
51 (
new-intersection-
set '(1 3 3 2 4) '(
2 3 2 2 4 5 1))
52 ;;;;;;;;;;;;;;;;adjion和union分别变为O(
1)和O(n)
53 ;;;;;;;;;;;;;;;;其他不变
转载于:https://www.cnblogs.com/tclan126/p/6477017.html
相关资源:数据结构—成绩单生成器
转载请注明原文地址: https://win8.8miu.com/read-1542096.html