(require-library lazy-lists simple-tests multi-methods) (import lazy-lists simple-tests methods) (effects-checked? #t) (define (cons-right var lst) (if (null? lst) (cons var lst) (cons (car lst) (cons-right var (cdr lst))))) (run-tests (define (First-five) (List 0 1 2 3 4)) (define (Fibs) (Append (List 0 1) (Lazy #f (Map + (Rest (Fibs)) (Fibs))))) "PREDICATES" (List? (First-five)) (List-finite? (First-five)) (List-not-null? (First-five)) (not (List-not-null? Nil)) (not (List-finite? (Cardinals))) (List? (Cardinals)) (= (Length-min (First-five) (Cardinals)) 5) (Lists-one-finite? (First-five) (Cardinals)) (not (Lists-one-finite? (Fibs) (Cardinals))) (= (Length (First-five)) 5) (= (Length (Rest (First-five))) 4) (eq? (Length (Rest (Cardinals))) #f) (= (Length (Take 5 (Cardinals))) 5) (eq? (Length (Cardinals)) #f) (eq? (Length (Drop 5 (Cardinals))) #f) (= (First (Drop 5 (Cardinals))) 5) (equal? (List->list (First-five)) '(0 1 2 3 4)) (equal? (List->list (Take 5 (Cardinals))) '(0 1 2 3 4)) (= (Length (Range 2 10)) (- 10 2)) (= (Length (Range 10)) 10) (= (Length (Range -1 10 2)) 6) (equal? (List->list (Range -1 10 2)) '(-1 1 3 5 7 9)) (equal? (List->list (Range 2 10)) '(2 3 4 5 6 7 8 9)) (equal? (List->list (Range 10 2)) '(10 9 8 7 6 5 4 3)) (equal? (receive (head index tail) (Split-with (cut < <> 3) (First-five)) (cons (First tail) (List->list head))) '(3 0 1 2)) (equal? (receive (head index tail) (Split-with (cut < <> 5) (Take 10 (Cardinals))) (append (List->list tail) (List->list head))) '(5 6 7 8 9 0 1 2 3 4)) (= (Count-while (cut < <> 2) (First-five)) 2) (= (Count-while (cut < <> 20) (First-five)) 5) (equal? (List->list (Take-while (cut < <> 5) (Take 10 (Cardinals)))) '(0 1 2 3 4)) (= (Length (Take-while (cut < <> 5) (Take 10 (Cardinals)))) 5) (= (Length (Drop-while (cut < <> 5) (Take 10 (Cardinals)))) 5) (= (First (Drop-while (cut < <> 5) (Take 10 (Cardinals)))) 5) (= (Length (Drop-while (cut < <> 2) (First-five))) 3) (= (First (Drop-while (cut < <> 2) (First-five))) 2) (equal? (List->list (Memp odd? (First-five))) '(1 2 3 4)) (equal? (List->list (Memv 5 (Take 10 (Cardinals)))) '(5 6 7 8 9)) (equal? (Assv 5 (Take 10 (Map (lambda (x) (list x x)) (Cardinals)))) '(5 5)) (eq? (Assv 10 (Map (lambda (x) (list x x)) (First-five))) #f) (eq? (Equal? (Cardinals) (Cardinals)) #f) (eq? (Equal? (Cardinals) (First-five)) #f) (eq? (Equal? (First-five) (First-five)) #t) (= (Length (Take 10 (Cardinals))) 10) (equal? (List->list (Take 5 (Filter odd? (Drop 1 (Cardinals))))) '(1 3 5 7 9)) (eq? (Length (Cardinals)) #f) (equal? (List->list (Map add1 (First-five))) '(1 2 3 4 5)) (equal? (List->list (Map + (First-five) (Take 5 (Cardinals)))) '(0 2 4 6 8)) (eq? (Length (Map + (Cardinals) (Cardinals))) #f) (= (Length (Filter odd? (First-five))) 2) (equal? (List->list (Filter odd? (First-five))) '(1 3)) (eq? (Length (Filter odd? (Cardinals))) #f) (= (Ref 20 (Sieve = (Zip (Cardinals) (Cardinals)))) 20) (equal? (List->list (Sieve = (Zip (First-five) (First-five)))) '(0 1 2 3 4)) (= (Ref 25 (Cardinals)) 25) (= (Ref 2 (First-five)) 2) (equal? (List->list (Repeat 3 #f)) '(#f #f #f)) (List-infinite? (Repeatedly (lambda () 1))) (equal? (List->list (Repeatedly 3 (lambda () 1))) '(1 1 1)) (List-infinite? (Iterate add1 0)) (List-finite? (Iterate 3 add1 0)) (equal? (List->list (Iterate 3 add1 0)) '(0 1 2)) (eq? (Length (Iterate add1 0)) #f) (equal? (List->list (Cycle 10 (First-five))) '(0 1 2 3 4 0 1 2 3 4)) (eq? (Length (Cycle (First-five))) #f) (= (Length (Append (First-five) (First-five))) 10) (not (Length (Append (Cardinals) (First-five)))) (equal? (List->list (Append (First-five) (First-five))) '(0 1 2 3 4 0 1 2 3 4)) (equal? (List->list (Take 12 (Append (First-five) (Cardinals)))) '(0 1 2 3 4 0 1 2 3 4 5 6)) (eq? (Length (Append (First-five) (Cardinals))) #f) (equal? (List->list (Reverse (First-five))) '(4 3 2 1 0)) (equal? (List->list (Reverse (Take 5 (Cardinals)))) '(4 3 2 1 0)) (= (Length (Reverse (First-five))) 5) (eq? (Length (Reverse* (Cardinals))) #f) (equal? (List->list (Ref 5 (Reverse* (Cardinals)))) '(5 4 3 2 1 0)) (equal? (List->list (Sort < (First-five))) '(0 1 2 3 4)) (Sorted? < (First-five)) (not (Sorted? < (Append (First-five) (First-five)))) (= (Length (Sort < (First-five))) 5) (equal? (List->list (Sort < (List 3 1 0 2 4))) '(0 1 2 3 4)) (equal? (receive (head tail) (Split-at 5 (Cardinals)) (cons (First tail) (List->list head))) '(5 0 1 2 3 4)) (equal? (receive (head tail) (Split-at 15 (Take 5 (Cardinals))) (append (List->list tail) (List->list head))) '(0 1 2 3 4)) "FOLDS" (= (Fold-left + 0 (Take 5 (Cardinals))) 10) (= (Fold-left + 0 (First-five) (First-five)) 20) (= (Fold-left * 1 (List 1 2 3 4)) 24) (equal? (Fold-left cons '() (Take 5 (Cardinals))) '(((((() . 0) . 1) . 2) . 3) . 4)) (equal? (Ref 4 (Fold-left* cons '() (Cardinals))) '(((((() . 0) . 1) . 2) . 3) . 4)) (= (Fold-right + 0 (Take 5 (Cardinals))) 10) (= (Fold-right + 0 (First-five) (First-five)) 20) (equal? (Fold-right cons '() (First-five)) '(0 1 2 3 4)) ; list (equal? (Fold-right cons '(a b c) (First-five)) '(0 1 2 3 4 a b c)) ; append (equal? (Ref 4 (Fold-right* cons '() (Cardinals))) '(4 3 2 1 0)) ; note changed order (equal? (Ref 4 (Fold-right* cons-right '() (Cardinals))) '(0 1 2 3 4)) (equal? (Ref 4 (Fold-right* cons '(a b c) (Cardinals))) '(4 3 2 1 0 a b c)) ; note changed order (equal? (Ref 4 (Fold-right* cons-right '(a b c) (Cardinals))) '(a b c 0 1 2 3 4)) "TRANSFORMATIONS" (equal? (List->list (vector->List '#(0 1 2 3 4))) '(0 1 2 3 4)) (Null? (vector->List '#())) (equal? (List->vector (Take 5 (Cardinals))) '#(0 1 2 3 4)) (equal? (List->vector (First-five)) '#(0 1 2 3 4)) (equal? (List->vector Nil) '#()) (eq? (Every? odd? (Take 15 (Filter odd? (Cardinals)))) #t) (eq? (Every? odd? (Take 15 (Cardinals))) #f) (eq? (Every? odd? Nil) #t) (eq? (Some? odd? Nil) #f) (eq? (Some? odd? (Take 5 (Filter even? (Cardinals)))) #f) (eq? (Some? odd? (First-five)) #t) "ZIP" (eq? (Length (Zip (Cardinals) (First-five))) #f) (eq? (Length (Zip (First-five) (Cardinals))) #f) (eq? (Length (Zip (Cardinals) (Cardinals))) #f) (= (Length (Zip (First-five) (First-five))) 10) (Eqv? (Take 14 (Zip (Cardinals) (First-five))) (List 0 0 1 1 2 2 3 3 4 4 5 6 7 8)) (Eqv? (Take 14 (Zip (Cardinals) (Cardinals))) (List 0 0 1 1 2 2 3 3 4 4 5 5 6 6)) (Eqv? (Zip (First-five) (First-five)) (List 0 0 1 1 2 2 3 3 4 4)) "PRIMES AND FIBS" (= (Ref 50 (Primes)) 233) (Eqv? (Take 5 (Primes)) (List 2 3 5 7 11)) (Eqv? (Take 10 (Fibs)) (List 0 1 1 2 3 5 8 13 21 34)) "LIST OF SUMS" (define (Sums Lst) (letrec ((sums (Cons 0 (Map + Lst (Lazy (Length Lst) sums))))) (Rest sums))) (equal? (List->list (Sums (First-five))) '(0 1 3 6 10)) "COMPUTE SQUARE ROOT BY NEWTON'S METHOD" (define (Within eps Lst) (let loop ((Lst Lst)) (let ((a (Ref 0 Lst)) (b (Ref 1 Lst))) (if (< (abs (- a b)) eps) b (loop (Rest Lst)))))) (define (Relative eps Lst) (let loop ((Lst Lst)) (let ((a (Ref 0 Lst)) (b (Ref 1 Lst))) (if (<= (abs (/ a b)) (* (abs b) eps)) b (loop (Rest Lst)))))) (define (Newton x) ; fixed point for square root (lambda (a) (/ (+ a (/ x a)) 2))) (let ((eps 0.000001)) (< (abs (- (sqrt 2) (Within eps (Iterate (Newton 2) 2)))) eps)) "IF NOT A THUNK A USE OF A LIST IMPLIES REALIZING" (define Integers (let loop ((n 1)) (Lazy #f (cons n (loop (+ n 1)))))) (List? Integers) (not (List-finite? Integers)) (not (Realized? Integers)) (= (Ref 5 Integers) 6) (Realized? Integers) )