2.59

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


(define (element-of-set? x set)
    (cond ((null? set) #f)
        ((equal? x (car set)) #t)
        (else (element-of-set? x (cdr set)))
    )
)

(element-of-set? 1 (list 1 2 3 4))
(define (union-set set1 set2)
    (cond
        ((null? set1) set2)
        ((null? set2) set1)
        ((element-of-set? (car set1) set2) (union-set (cdr set1) set2))
        (else (cons (car set1) (union-set (cdr set1) set2)))
    )
)

; 期待得到 (1 2 3 4 8)
(union-set (list 1 2 3 4) (list 1 2 3 8))

results matching ""

    No results matching ""