Changeset 29260 in project


Ignore:
Timestamp:
06/27/13 11:35:17 (8 years ago)
Author:
juergen
Message:

examples in lazy-lists changed

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/lazy-lists

    r29252 r29260  
    464464<enscript highlight=scheme>
    465465
    466 (require-library lazy-lists simple-tests)
    467 (import lazy-lists)
     466(require-library lazy-lists multi-methods)
     467(import lazy-lists methods)
    468468
    469469(define (cons-right var lst)
     
    497497  (Append (List 0 1) (Lazy #f (Map + (Rest (Fibs)) (Fibs)))))
    498498
    499 ;(define port (open-input-file "lazy-lists.scm"))
    500 ;(define input (input->List port read-line))
    501 
    502 (run-tests
    503   (= (Length (First-five)) 5)
    504   (= (Length (Rest (First-five))) 4)
    505   (eq? (Length (Rest (Cardinals))) #f)
    506   (= (Length (Take 5 (Cardinals))) 5)
    507   (eq? (Length (Cardinals)) #f)
    508   (eq? (Length (Drop 5 (Cardinals))) #f)
    509   (= (First (Drop 5 (Cardinals))) 5)
    510   (equal? (List->list (First-five)) '(0 1 2 3 4))
    511   (equal? (List->list (Take 5 (Cardinals))) '(0 1 2 3 4))
    512   (= (Length (Interval 2 10)) (- 10 2))
    513   (equal? (List->list (Interval 2 10)) '(2 3 4 5 6 7 8 9))
    514   (equal? (List->list (Interval 10 2)) '(10 9 8 7 6 5 4 3))
    515   (equal?
    516     (receive (head index tail) (Split-with (cut = <> 3) (First-five))
    517       (cons (First tail) (List->list head)))
    518     '(3 0 1 2))
    519   (equal?
    520     (receive (head index tail) (Split-with (cut = <> 5) (Take 10 (Cardinals)))
    521       (append (List->list tail) (List->list head)))
    522     '(5 6 7 8 9 0 1 2 3 4))
    523   (= (Index (cut = <> 2) (First-five)) 2)
    524   (= (Index (cut = <> 20) (First-five)) 5)
    525   (equal? (List->list (Take-upto (cut = <> 5) (Take 10 (Cardinals))))
    526        '(0 1 2 3 4))
    527   (= (Length (Take-upto (cut = <> 5) (Take 10 (Cardinals)))) 5)
    528   (= (Length (Drop-upto (cut = <> 5) (Take 10 (Cardinals)))) 5)
    529   (= (First (Drop-upto (cut = <> 5) (Take 10 (Cardinals)))) 5)
    530   (= (Length (Drop-upto (cut = <> 2) (First-five))) 3)
    531   (= (First (Drop-upto (cut = <> 2) (First-five))) 2)
    532   (equal? (List->list (Memp odd? (First-five))) '(1 2 3 4))
    533   (equal? (List->list (Memv 5 (Take 10 (Cardinals)))) '(5 6 7 8 9))
    534   (equal? (Assv 5 (Take 10 (Map (lambda (x) (list x x)) (Cardinals))))
    535     '(5 5))
    536   (eq? (Assv 10 (Map (lambda (x) (list x x)) (First-five))) #f)
    537   (eq? (Equal? (Cardinals) (Cardinals)) #f)
    538   (eq? (Equal? (Cardinals) (First-five)) #f)
    539   (eq? (Equal? (First-five) (First-five)) #t)
    540   (= (Length (Take 10 (Cardinals))) 10)
    541   (equal? (List->list (Take 5 (Filter odd? (Drop 1 (Cardinals)))))
    542     '(1 3 5 7 9))
    543   (eq? (Length (Cardinals)) #f)
    544   (equal? (List->list (Map add1 (First-five))) '(1 2 3 4 5))
    545   (equal? (List->list (Map + (First-five) (Take 5 (Cardinals))))
    546     '(0 2 4 6 8))
    547   (eq? (Length (Map + (Cardinals) (Cardinals))) #f)
    548   (= (Length (Filter odd? (First-five))) 2)
    549   (equal? (List->list (Filter odd? (First-five))) '(1 3))
    550   (eq? (Length (Filter odd? (Cardinals))) #f)
    551   (= (Ref 20 (Sieve = (Zip (Cardinals) (Cardinals)))) 20)
    552   (equal? (List->list (Sieve = (Zip (First-five) (First-five))))
    553     '(0 1 2 3 4))
    554   (= (Ref 25 (Cardinals)) 25)
    555   (= (Ref 2 (First-five)) 2)
    556   (equal? (List->list (Take 3 (Repeat #f))) '(#f #f #f))
    557   (equal? (List->list (Take 3 (Repeatedly (lambda () 1))))
    558     '(1 1 1))
    559   (equal? (List->list (Take 3 (Iterate add1 0))) '(0 1 2))
    560   (eq? (Length (Iterate add1 0)) #f)
    561   (= (Length (Append (First-five) (First-five))) 10)
    562   (equal? (List->list  (Append (First-five) (First-five)))
    563     '(0 1 2 3 4 0 1 2 3 4))
    564   (equal? (List->list (Take 12 (Append (First-five) (Cardinals))))
    565     '(0 1 2 3 4 0 1 2 3 4 5 6))
    566   (eq? (Length (Append (First-five) (Cardinals))) #f)
    567   (equal? (List->list (Reverse (First-five))) '(4 3 2 1 0))
    568   (equal? (List->list (Reverse (Take 5 (Cardinals)))) '(4 3 2 1 0))
    569   (= (Length (Reverse (First-five))) 5)
    570   (eq? (Length (Reverse* (Cardinals))) #f)
    571   (equal? (List->list (Ref 5 (Reverse* (Cardinals)))) '(5 4 3 2 1 0))
    572   (equal? (List->list (Take 10 (Cycle (First-five))))
    573     '(0 1 2 3 4 0 1 2 3 4))
    574   (eq? (Length (Cycle (First-five))) #f)
    575   (equal? (List->list (Sort < (First-five))) '(0 1 2 3 4))
    576   (= (Length (Sort < (First-five))) 5)
    577   (equal? (List->list (Sort < (List 3 1 0 2 4))) '(0 1 2 3 4))
    578   (equal?
    579     (receive (head tail) (Split-at 5 (Cardinals))
    580       (cons (First tail) (List->list head)))
    581     '(5 0 1 2 3 4))
    582   (equal?
    583     (receive (head tail) (Split-at 15 (Take 5 (Cardinals)))
    584       (append (List->list tail) (List->list head)))
    585     '(0 1 2 3 4))
    586   (= (Fold-left + 0 (Take 5 (Cardinals))) 10)
    587   (= (Fold-left + 0 (First-five) (First-five)) 20)
    588   (= (Fold-left * 1 (List 1 2 3 4)) 24)
    589   (equal? (Fold-left cons '() (Take 5 (Cardinals)))
    590     '(((((() . 0) . 1) . 2) . 3) . 4))
    591   (equal? (Ref 4 (Fold-left* cons '() (Cardinals)))
    592     '(((((() . 0) . 1) . 2) . 3) . 4))
    593   (= (Fold-right + 0 (Take 5 (Cardinals))) 10)
    594   (= (Fold-right + 0 (First-five) (First-five)) 20)
    595   (equal? (Fold-right cons '() (First-five))
    596     '(0 1 2 3 4)) ; list
    597   (equal? (Fold-right cons '(a b c) (First-five))
    598     '(0 1 2 3 4 a b c)) ; append
    599   (equal? (Ref 4 (Fold-right* cons '() (Cardinals)))
    600     '(4 3 2 1 0)) ; note changed order
    601   (equal? (Ref 4 (Fold-right* cons-right '() (Cardinals)))
    602     '(0 1 2 3 4))
    603   (equal? (Ref 4 (Fold-right* cons '(a b c) (Cardinals)))
    604     '(4 3 2 1 0 a b c)) ; note changed order
    605   (equal? (Ref 4 (Fold-right* cons-right '(a b c) (Cardinals)))
    606     '(a b c 0 1 2 3 4))
    607   (equal? (List->list (vector->List '#(0 1 2 3 4))) '(0 1 2 3 4))
    608   (Null? (vector->List '#()))
    609   (equal? (List->vector (Take 5 (Cardinals))) '#(0 1 2 3 4))
    610   (equal? (List->vector (First-five)) '#(0 1 2 3 4))
    611   (equal? (List->vector Nil) '#())
    612   (eq? (Every? odd? (Take 15 (Filter odd? (Cardinals)))) #t)
    613   (eq? (Every? odd? (Take 15 (Cardinals))) #f)
    614   (eq? (Every? odd? Nil) #t)
    615   (eq? (Some? odd? Nil) #f)
    616   (eq? (Some? odd? (Take 5 (Filter even? (Cardinals)))) #f)
    617   (eq? (Some? odd? (First-five)) #t)
    618   (eq? (Length (Zip (Cardinals) (First-five))) #f)
    619   (eq? (Length (Zip (First-five) (Cardinals))) #f)
    620   (eq? (Length (Zip (Cardinals) (Cardinals))) #f)
    621   (= (Length (Zip (First-five) (First-five))) 10)
    622   (Eqv? (Take 14 (Zip (Cardinals) (First-five)))
    623     (List 0 0 1 1 2 2 3 3 4 4 5 6 7 8))
    624   (Eqv? (Take 14 (Zip (Cardinals) (Cardinals)))
    625     (List 0 0 1 1 2 2 3 3 4 4 5 5 6 6))
    626   (Eqv? (Zip (First-five) (First-five))
    627     (List 0 0 1 1 2 2 3 3 4 4))
    628   (= (Ref 50 (Primes)) 233)
    629   (Eqv? (Take 5 (Primes)) (List 2 3 5 7 11))
    630   (Eqv? (Take 10 (Fibs)) (List  0 1 1 2 3 5 8 13 21 34))
    631   ;; compute square root
    632   (let ((eps 0.000001))
    633     (< (abs (- (sqrt 2) (Within eps (Iterate (Newton 2) 2)))) eps))
    634   (equal? (List->list (Sums (First-five))) '(0 1 3 6 10))
    635   )
     499;; some tests
     500(Length (First-five)) ;-> 5
     501(Length (Rest (First-five))) ;-> 4
     502(Length (Rest (Cardinals))) ;-> #f
     503(Length (Take 5 (Cardinals))) ;-> 5
     504(Length (Cardinals)) ;-> #f
     505(Length (Drop 5 (Cardinals))) ;-> #f
     506(First (Drop 5 (Cardinals))) ;-> 5
     507(List->list (First-five)) ;-> '(0 1 2 3 4)
     508(List->list (Take 5 (Cardinals))) ;-> '(0 1 2 3 4)
     509(Length (Interval 2 10)) ;-> (- 10 2)
     510(List->list (Interval 2 10)) ;-> '(2 3 4 5 6 7 8 9)
     511(List->list (Interval 10 2)) ;-> '(10 9 8 7 6 5 4 3)
     512(receive (head index tail) (Split-with (cut = <> 3) (First-five))
     513        (cons (First tail) (List->list head))) ;-> '(3 0 1 2)
     514(receive (head index tail) (Split-with (cut = <> 5) (Take 10 (Cardinals)))
     515        (append (List->list tail) (List->list head))) ;-> '(5 6 7 8 9 0 1 2 3 4)
     516(Index (cut = <> 2) (First-five)) ;-> 2
     517(Index (cut = <> 20) (First-five)) ;-> 5
     518(List->list (Take-upto (cut = <> 5) (Take 10 (Cardinals))))
     519     ;-> '(0 1 2 3 4)
     520(Length (Take-upto (cut = <> 5) (Take 10 (Cardinals)))) ;-> 5
     521(Length (Drop-upto (cut = <> 5) (Take 10 (Cardinals)))) ;-> 5
     522(First (Drop-upto (cut = <> 5) (Take 10 (Cardinals)))) ;-> 5
     523(Length (Drop-upto (cut = <> 2) (First-five))) ;-> 3
     524(First (Drop-upto (cut = <> 2) (First-five))) ;-> 2
     525(List->list (Memp odd? (First-five))) ;-> '(1 2 3 4)
     526(List->list (Memv 5 (Take 10 (Cardinals)))) ;-> '(5 6 7 8 9)
     527(Assv 5 (Take 10 (Map (lambda (x) (list x x)) (Cardinals))))
     528  ;-> '(5 5)
     529(Assv 10 (Map (lambda (x) (list x x)) (First-five))) ;-> #f
     530(Equal? (Cardinals) (Cardinals)) ;-> #f
     531(Equal? (Cardinals) (First-five)) ;-> #f
     532(Equal? (First-five) (First-five)) ;-> #t
     533(Length (Take 10 (Cardinals))) ;-> 10
     534(List->list (Take 5 (Filter odd? (Drop 1 (Cardinals)))))
     535  ;-> '(1 3 5 7 9)
     536(Length (Cardinals)) ;-> #f
     537(List->list (Map add1 (First-five))) ;-> '(1 2 3 4 5)
     538(List->list (Map + (First-five) (Take 5 (Cardinals))))
     539  ;-> '(0 2 4 6 8)
     540(Length (Map + (Cardinals) (Cardinals))) ;-> #f
     541(Length (Filter odd? (First-five))) ;-> 2
     542(List->list (Filter odd? (First-five))) ;-> '(1 3)
     543(Length (Filter odd? (Cardinals))) ;-> #f
     544(Ref 20 (Sieve = (Zip (Cardinals) (Cardinals)))) ;-> 20
     545(List->list (Sieve = (Zip (First-five) (First-five))))
     546  ;-> '(0 1 2 3 4)
     547(Ref 25 (Cardinals)) ;-> 25
     548(Ref 2 (First-five)) ;-> 2
     549(List->list (Take 3 (Repeat #f))) ;-> '(#f #f #f)
     550(List->list (Take 3 (Repeatedly (lambda () 1))))
     551  ;-> '(1 1 1)
     552(List->list (Take 3 (Iterate add1 0))) ;-> '(0 1 2)
     553(Length (Iterate add1 0)) ;-> #f
     554(Length (Append (First-five) (First-five))) ;-> 10
     555(List->list  (Append (First-five) (First-five)))
     556  ;-> '(0 1 2 3 4 0 1 2 3 4)
     557(List->list (Take 12 (Append (First-five) (Cardinals))))
     558  ; -> '(0 1 2 3 4 0 1 2 3 4 5 6)
     559(Length (Append (First-five) (Cardinals))) ; -> #f
     560(List->list (Reverse (First-five))) ; -> '(4 3 2 1 0)
     561(List->list (Reverse (Take 5 (Cardinals)))) ; -> '(4 3 2 1 0)
     562(Length (Reverse (First-five))) ; -> 5
     563(Length (Reverse* (Cardinals))) ; -> #f
     564(List->list (Ref 5 (Reverse* (Cardinals)))) ; -> '(5 4 3 2 1 0)
     565(List->list (Take 10 (Cycle (First-five))))
     566  ; -> '(0 1 2 3 4 0 1 2 3 4)
     567(Length (Cycle (First-five))) ; -> #f
     568(List->list (Sort < (First-five))) ; -> '(0 1 2 3 4)
     569(Length (Sort < (First-five))) ; -> 5
     570(List->list (Sort < (List 3 1 0 2 4))) ; -> '(0 1 2 3 4)
     571(receive (head tail) (Split-at 5 (Cardinals))
     572        (cons (First tail) (List->list head)))
     573; -> '(5 0 1 2 3 4)
     574(receive (head tail) (Split-at 15 (Take 5 (Cardinals)))
     575        (append (List->list tail) (List->list head)))
     576; -> '(0 1 2 3 4)
     577(Fold-left + 0 (Take 5 (Cardinals))) ; -> 10
     578(Fold-left + 0 (First-five) (First-five)) ; -> 20
     579(Fold-left * 1 (List 1 2 3 4)) ; -> 24
     580(Fold-left cons '() (Take 5 (Cardinals)))
     581  ; -> '(((((() . 0) . 1) . 2) . 3) . 4)
     582(Ref 4 (Fold-left* cons '() (Cardinals)))
     583  ; -> '(((((() . 0) . 1) . 2) . 3) . 4)
     584(Fold-right + 0 (Take 5 (Cardinals))) ; -> 10
     585(Fold-right + 0 (First-five) (First-five)) ; -> 20
     586;; list
     587(Fold-right cons '() (First-five))
     588  ; -> '(0 1 2 3 4)
     589;; append
     590(Fold-right cons '(a b c) (First-five))
     591  ; -> '(0 1 2 3 4 a b c)
     592(Ref 4 (Fold-right* cons '() (Cardinals)))
     593  ; -> '(4 3 2 1 0)) ; note changed order
     594(Ref 4 (Fold-right* cons-right '() (Cardinals)))
     595  ; -> '(0 1 2 3 4)
     596(Ref 4 (Fold-right* cons '(a b c) (Cardinals)))
     597  ; -> '(4 3 2 1 0 a b c) ; note changed order
     598(Ref 4 (Fold-right* cons-right '(a b c) (Cardinals)))
     599  ; -> '(a b c 0 1 2 3 4)
     600(List->list (vector->List '#(0 1 2 3 4))) ; -> '(0 1 2 3 4)
     601(Null? (vector->List '#())) ; -> #t
     602(List->vector (Take 5 (Cardinals))) ; -> '#(0 1 2 3 4)
     603(List->vector (First-five)) ; -> '#(0 1 2 3 4)
     604(List->vector Nil) ; -> '#()
     605(Every? odd? (Take 15 (Filter odd? (Cardinals)))) ; -> #t
     606(Every? odd? (Take 15 (Cardinals))) ; -> #f
     607(Every? odd? Nil) ; -> #t
     608(Some? odd? Nil) ; -> #f
     609(Some? odd? (Take 5 (Filter even? (Cardinals)))) ; -> #f
     610(Some? odd? (First-five)) ; -> #t
     611(Length (Zip (Cardinals) (First-five))) ; -> #f
     612(Length (Zip (First-five) (Cardinals))) ; -> #f
     613(Length (Zip (Cardinals) (Cardinals))) ; -> #f
     614(Length (Zip (First-five) (First-five))) ; -> 10
     615(Eqv? (Take 14 (Zip (Cardinals) (First-five)))
     616      (List 0 0 1 1 2 2 3 3 4 4 5 6 7 8)) ; -> #t
     617(Eqv? (Take 14 (Zip (Cardinals) (Cardinals)))
     618      (List 0 0 1 1 2 2 3 3 4 4 5 5 6 6)) ; -> #t
     619(Eqv? (Zip (First-five) (First-five))
     620      (List 0 0 1 1 2 2 3 3 4 4)) ; -> #t
     621(Ref 50 (Primes)) ; -> 233
     622(Eqv? (Take 5 (Primes)) (List 2 3 5 7 11)) ; -> #t
     623(Eqv? (Take 10 (Fibs)) (List  0 1 1 2 3 5 8 13 21 34)) ; -> #t
     624;; compute square root
     625(let ((eps 0.000001))
     626  (< (abs (- (sqrt 2) (Within eps (Iterate (Newton 2) 2)))) eps)) ; -> #t
     627(List->list (Sums (First-five))) ; -> '(0 1 3 6 10)
    636628
    637629</enscript>
Note: See TracChangeset for help on using the changeset viewer.