乐正

Actions speak louder than words.

Sicp-ex2-59

问题

请为采用为排序表的集合实现定义union-set操作。

解答

练习2.59 (ex2-59.scm) download
1
2
3
4
5
6
(define (union-set set1 set2)
  (cond ((and (null? set1) (null? set2)) '())
        ((null? set1) set2)
        ((element-of-set? (car set1) set2)
         (union-set (cdr set1) set2))
        (else (union-set (cdr set1) (cons (car set1) set2)))))

测试

1
2
3
4
5
6
7
8
;; (load sicp/p103-element-of-set?.scm)
;; (load sicp/ex2-59.scm)

(union-set '(1 3 4) '(2 3 9))
;Value 24: (4 1 2 3 9)

(union-set (list 3 4 'x 'y) (list 'z 'x 3 1))
;Value 25: (y 4 z x 3 1)

draft

« sicp-ex2-58 sicp-ex2-60 »

Comments