Created
November 30, 2015 00:43
-
-
Save ruandao/0a35f376cdf66f096195 to your computer and use it in GitHub Desktop.
sicp 2.65 求O(n) 的union-set 以及 intersection-set 操作
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
| ;#lang planet neil/sicp | |
| #lang racket | |
| (require (planet soegaard/sicp:2:1/sicp)) | |
| (define wave einstein) | |
| (define (union-set set1 set2) | |
| (define (union-list l1 l2) | |
| (cond ((null? l1) l2) | |
| ((null? l2) l1) | |
| ((> (car l1) (car l2)) (cons (car l1) (union-list (cdr l1) l2))) | |
| ((= (car l1) (car l2)) (cons (car l1) (union-list (cdr l1) (cdr l2)))) | |
| ((< (car l1) (car l2)) (cons (car l2) (union-list l1 (cdr l2)))))) | |
| (let ((list-1 (tree-list-2 set1)) | |
| (list-2 (tree-list-2 set2))) | |
| (list->tree (union-list list-1 list-2)))) | |
| (define (intersection-set s1 s2) | |
| (define (intersection-list l1 l2) | |
| (cond ((null? l1) '()) | |
| ((null? l2) '()) | |
| ((> (car l1) (car l2)) (intersection-list (cdr l1) l2)) | |
| ((= (car l1) (car l2)) (cons (car l1) (intersection-list (cdr l1) (cdr l2)))) | |
| ((< (car l1) (car l2)) (intersection-list l1 (cdr l2))))) | |
| (let ((l1 (tree->list-2 s1)) | |
| (l2 (tree->list-2 s2))) | |
| (list->tree (intersection-list l1 l2)))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment