Browse Source

adjoin-set for ordered list representation of set

master
parent
commit
116004921e
  1. 39
      2.3/2.60.scm
  2. 44
      2.3/2.61.scm

39
2.3/2.60.scm

@ -0,0 +1,39 @@
(load "displaylib.scm")
(title "Exercise 2.60")
(print "
We specified that a set would be represented as a list with no duplicates. Now suppose we allow duplicates. For instance, the set {1,2,3} could be represented as the list (2 3 2 1 3 2 2). Design procedures element-of-set?, adjoin-set, union-set, and intersection-set that operate on this representation. How does the efficiency of each compare with the corresponding procedure for the non-duplicate representation? Are there applications for which you would use this representation in preference to the non-duplicate one?
")
; -- given lib --
(define (element-of-set? x set)
(cond ((null? set) false)
((equal? x (car set)) true)
(else (element-of-set? x (cdr set)))))
(define (intersection-set set1 set2)
(cond ((or (null? set1) (null? set2)) '())
((element-of-set? (car set1) set2)
(cons (car set1)
(intersection-set (cdr set1) set2)))
(else (intersection-set (cdr set1) set2))))
(define (adjoin-set x set) (cons x set))
; -- START --
(define (union-set e f) (append e f))
(define set1 '(a b c))
(define set2 '(a x y))
(display "set1 = '")(display set1)(newline)
(display "set2 = '")(display set2)(newline)
(display "(union-set set1 set2)")(newline)
(display (union-set set1 set2))(newline)
(display "(intersection-set set1 set2)")(newline)
(display (intersection-set set1 set2))(newline)
(display "(adjoin-set 'a set1)")(newline)
(display (adjoin-set 'a set1))(newline)
; Worse if you have to manage a lot of duplicate values.
; Better if you have to manage very few duplicate values (sparse set).

44
2.3/2.61.scm

@ -0,0 +1,44 @@
(load "displaylib.scm")
(title "Exercise 2.61")
(print "
Give an implementation of adjoin-set using the ordered representation. By analogy with element-of-set? show how to take advantage of the ordering to produce a procedure that requires on the average about half as many steps as with the unordered representation.
")
; -- given lib --
(define (element-of-set? x set)
(cond ((null? set) false)
((= x (car set)) true)
((< x (car set)) false)
(else (element-of-set? x (cdr 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))
((< x2 x1)
(intersection-set set1 (cdr set2)))))))
; -- START --
(define (adjoin-set x set)
(cond ((null? set) (list x))
((> x (car set))
(cons (car set) (adjoin-set x (cdr set))))
((= x (car set)) set)
((< x (car set)) (cons x set))))
(define set1 '(10 20 30))
(define set2 '(20 40 50))
(display "set1 = '")(display set1)(newline)
(display "set2 = '")(display set2)(newline)
(display "(intersection-set set1 set2)")(newline)
(display (intersection-set set1 set2))(newline)
(display "(adjoin-set 30 set1)")(newline)
(display (adjoin-set 30 set1))(newline)
(display "(adjoin-set 25 set1)")(newline)
(display (adjoin-set 25 set1))(newline)
Loading…
Cancel
Save