(require-library lazy-lists) (import lazy-lists) ;;; (run xpr0 xpr1 ...) ;;; ------------------- (define (run . xprs) (let loop ((xprs xprs)) (if (null? xprs) (print "All tests passed!") (if (car xprs) (loop (cdr xprs)) (error 'run "#### Some test failed! ####"))))) (define (cons-right var lst) (if (null? lst) (cons var lst) (cons (car lst) (cons-right var (cdr lst))))) (define (Within eps seq) (let loop ((seq seq)) (let ((a (Ref 0 seq)) (b (Ref 1 seq))) (if (< (abs (- a b)) eps) b (loop (Rest seq)))))) (define (Relative eps seq) (let loop ((seq seq)) (let ((a (Ref 0 seq)) (b (Ref 1 seq))) (if (<= (abs (/ a b)) (* (abs b) eps)) b (loop (Rest seq)))))) (define (Newton x) ; fixed point for square root (lambda (a) (/ (+ a (/ x a)) 2))) (define (Sums seq) ; List of sums (letrec ((sums (Cons 0 (Map + seq (Lazy (Length seq) sums))))) (Rest sums))) (define (First-five) (List 0 1 2 3 4)) (define (Fibs) (Append (List 0 1) (Lazy #f (Map + (Rest (Fibs)) (Fibs))))) ;(define port (open-input-file "lazy-lists.scm")) ;(define input (input->List port read-line)) (run (= (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 (Interval 2 10)) (- 10 2)) (equal? (List->list (Interval 2 10)) '(2 3 4 5 6 7 8 9)) (equal? (List->list (Interval 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)) (= (Index (cut = <> 2) (First-five)) 2) (= (Index (cut = <> 20) (First-five)) 5) (equal? (List->list (Take-upto (cut = <> 5) (Take 10 (Cardinals)))) '(0 1 2 3 4)) (= (Length (Take-upto (cut = <> 5) (Take 10 (Cardinals)))) 5) (= (Length (Drop-upto (cut = <> 5) (Take 10 (Cardinals)))) 5) (= (First (Drop-upto (cut = <> 5) (Take 10 (Cardinals)))) 5) (= (Length (Drop-upto (cut = <> 2) (First-five))) 3) (= (First (Drop-upto (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 (Take 3 (Repeat #f))) '(#f #f #f)) (equal? (List->list (Take 3 (Repeatedly (lambda () 1)))) '(1 1 1)) (equal? (List->list (Take 3 (Iterate add1 0))) '(0 1 2)) (eq? (Length (Iterate add1 0)) #f) (= (Length (Append (First-five) (First-five))) 10) (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 (Take 10 (Cycle (First-five)))) '(0 1 2 3 4 0 1 2 3 4)) (eq? (Length (Cycle (First-five))) #f) (equal? (List->list (Sort < (First-five))) '(0 1 2 3 4)) (= (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)) (= (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)) (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) (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)) (= (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)) ;; compute square root (let ((eps 0.000001)) (< (abs (- (sqrt 2) (Within eps (Iterate (Newton 2) 2)))) eps)) (equal? (List->list (Sums (First-five))) '(0 1 3 6 10)) )