sicp-solutions/chapter-2/ex-2.59.scm

15 lines
449 B
Scheme

#lang sicp
(define (element-of-set? x set)
(cond ((null? set) false)
((equal? x (car set)) true)
(else (element-of-set? x (cdr set)))))
; we will simply adjoin elements of set1 to set2 if they are not there
; and drop them if they are there
(define (union-set set1 set2)
(cond ((null? set1) set2)
((null? set2) set1)
((element-of-set? (car set1) set2)
(union-set (cdr set1) set2))
(else (union-set (cdr set1) (cons (car set1) set2)))))