Created
December 19, 2014 08:06
-
-
Save zestime/540ba5a50dbc7e421038 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (define (element-of-set? x set) | |
| (cond ((null? set) false) | |
| ((equal? x (car set)) true) | |
| (else (element-of-set? x (cdr set))))) | |
| (define (adjoin-set x set) | |
| (if (element-of-set? x set) | |
| set | |
| (cons x 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 (union-set set1 set2) | |
| (cond ((null? set1) set2) | |
| ((null? set2) set1) | |
| (else | |
| (if (element-of-set? (car set1) set2) | |
| (union-set (cdr set1) set2) | |
| (union-set (cdr set1) (cons (car set1) set2)))) | |
| )) | |
| ;; define variables for testing | |
| (define set1 '(1 2 3)) | |
| (define set2 '(2 3 4)) | |
| ; adjoin | |
| (adjoin-set 3 set1) ; '(1 2 3) | |
| (adjoin-set 4 set1) ; '(4 1 2 3) | |
| ; intersection | |
| (intersection-set set1 set2) ; '(2 3) | |
| ; union | |
| (union-set set1 '()) ; '(1 2 3) | |
| (union-set set1 set2) ; '(1 2 3 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment