Implement till 2.62

main
Felix Martin 2020-11-15 11:30:56 -05:00
parent d343170831
commit 3c321654fb
3 changed files with 126 additions and 4 deletions

View File

@ -198,5 +198,5 @@
(newtons-method (lambda (y) (- (* y y) x)) 1.0))
(newline)
(display "example - Newton's Method")
(display "example - Newton's Method") (newline)
(display (sqrt 3)) (newline)

View File

@ -100,7 +100,7 @@
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
(fold-right op initial (cdr sequence)))))
(display (fold-right / 1 (list 1 2 3))) (newline) ; 3/2
(display (fold-left / 1 (list 1 2 3))) (newline) ; 1/6

View File

@ -1,6 +1,128 @@
(load "util.scm")
(display "\nex-2.59") (newline)
(display "example - set via unordered list\n")
(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 s1 s2)
(cond
((or (null? s1) (null? s2)) '())
((element-of-set? (car s1) s2) (cons (car s1) (intersection-set (cdr s1) s2)))
(else (intersection-set (cdr s1) s2))))
(define s '(3 1 5))
(display (element-of-set? 3 s)) (newline)
(display (element-of-set? 4 s)) (newline)
(display (adjoin-set 3 s)) (newline)
(display (adjoin-set 4 s)) (newline)
(display (intersection-set s '(3 1))) (newline)
(display "\nex-2.59 - union-set") (newline)
(define (union-set s1 s2)
(cond
((null? s1) s2)
((element-of-set? (car s1) s2) (union-set (cdr s1) s2))
(else (union-set (cdr s1) (cons (car s1) s2)))))
(display (union-set '(3 2 1) '(5 3))) (newline)
(display "\nex-2.60 - set via list with duplicates") (newline)
; Does not change, but runtime increases because we have to iterate over the
; duplicates.
(define element-of-set?-dup element-of-set?)
(define s '(3 1 3 5))
(display (element-of-set?-dup 3 s)) (newline)
(display (element-of-set?-dup 4 s)) (newline)
; Runtime is constant.
(define (adjoin-set-dup x set) (cons x set))
(display (adjoin-set 3 s)) (newline)
(display (adjoin-set 4 s)) (newline)
; Also stays the same, but becomes more expensive because of duplicates.
(define intersection-set-dup intersection-set)
(display (intersection-set s '(3 1))) (newline)
; We could also implement a version that de-duplicates but becomes even more
; expensive.
(define (intersection-set-dup s1 s2)
(if (or (null? s1) (null? s2))
'()
(let ((rest (intersection-set-dup (cdr s1) s2)))
(if (and (element-of-set?-dup (car s1) s2)
(not (element-of-set?-dup (car s1) rest)))
(adjoin-set-dup (car s1) rest)
rest))))
(display (intersection-set-dup s '(3 1))) (newline)
(define union-set-dup append)
(display (union-set-dup '(3 2 1) '(5 3))) (newline)
; Applications that do a lot of adjoin and union operations would benefit from
; this representation because they run in O(1) and O(n) runtime respectively.
(display "\nex-2.61 - set via ordered list") (newline)
(define (element-of-set? x set)
(cond ((null? set) false)
((= x (car set)) true)
((< x (car set)) false)
(else (element-of-set? x (cdr set)))))
(define (intersection-set s1 s2)
(if (or (null? s1) (null? s2))
'()
(let ((x1 (car s1)) (x2 (car s2)))
(cond
((= x1 x2) (cons x1 (intersection-set (cdr s1) (cdr s2))))
((< x1 x2) (intersection-set (cdr s1) s2))
((> x1 x2) (intersection-set s1 (cdr s2)))))))
(define (adjoin-set x set)
(cond
((null? set) (list x))
((= x (car set)) set)
((< x (car set)) (cons x set))
(else (cons (car set) (adjoin-set x (cdr set))))))
(define s '(1 3 5))
(display (element-of-set? 3 s)) (newline)
(display (element-of-set? 4 s)) (newline)
(display (adjoin-set 3 s)) (newline)
(display (adjoin-set 4 s)) (newline)
(display (adjoin-set 4 '())) (newline)
(display (adjoin-set 8 s)) (newline)
(display (intersection-set s '(1 3))) (newline)
(display "\nex-2.62") (newline)
(define (union-set s1 s2)
(cond
((null? s1) s2)
((null? s2) s1)
(else
(let ((x1 (car s1)) (x2 (car s2)))
(cond
((= x1 x2) (cons x1 (union-set (cdr s1) (cdr s2))))
((< x1 x2) (cons x1 (union-set (cdr s1) s2)))
((> x1 x2) (cons x2 (union-set s1 (cdr s2)))))))))
(display (union-set '(1 3 5 6 7) '(2 3 4 10))) (newline)
(display "\nex-2.63") (newline)
(display "\nex-2.60") (newline)