﻿; presumes Set and setdefs (define (ri) (inexact->exact (floor (* (random) 10000000)))) ; (define (rs n) (if (zero? n) empty (add (ri) (rs (- n 1))))) ; slow? (define (rs n) (let bs ((s empty)(n n)) (if (zero? n) s (bs (add (ri) s) (- n 1))))) ; 24 sec for 10^6 (define b1 (rs 100000)) (define b2 (rs 100000)) (define b3 (rs 100000)) (define (Union a b)(if (null? a) b (if (null? b) a (if (= (car a) (car b)) (cons (car a) (Union (cdr a)(cdr b))) (if (< (car a) (car b)) (cons (car a) (Union (cdr a) b)) (cons (car b) (Union a (cdr b)))))))) (define (Inter a b)(if (or (null? a) (null? b)) () (if (= (car a) (car b)) (cons (car a) (Inter (cdr a)(cdr b))) (if (< (car a) (car b)) (Inter (cdr a) b) (Inter a (cdr b)))))) (define (l2t l) (if (null? l) empty (add (car l) (l2t (cdr l))))) (and (= (cardinal (union (inter b1 b2) b3)) (cardinal (inter (union b1 b3) (union b2 b3)))) (equal (l2t (elements b2)) b2) (equal? (elements (union b1 b2)) (Union (elements b1) (elements b2))) (equal? (elements (inter b1 b2)) (Inter (elements b1) (elements b2))) (equal (union (inter b1 b2) b3) (inter (union b1 b3) (union b2 b3))) ; => #t (equal (inter (union b1 b2) b3) (union (inter b1 b3) (inter b2 b3))) ; => #t (equal (inter b1 b3) (inter b3 b1)) ) ; => #t