Changeset 27201 in project


Ignore:
Timestamp:
08/05/12 14:07:20 (9 years ago)
Author:
juergen
Message:

lazy-lists version 0.2 without dependency on clojurian

File:
1 edited

Legend:

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

    r27160 r27201  
    720720<enscript highlight=scheme>
    721721
    722 
    723 (require-library clojurian-syntax lazy-lists contracts)
    724 (import lazy-lists clojurian-syntax contracts)
     722(require-library lazy-lists)
     723(import lazy-lists)
    725724
    726725;;; (run xpr0 xpr1 ...)
     
    764763  (Append (List 0 1) (Lazy #f (Map + (Rest (Fibs)) (Fibs)))))
    765764
    766 (define port (open-input-file "lazy-lists.scm"))
    767 (define input (input->List port read-line))
     765;(define port (open-input-file "lazy-lists.scm"))
     766;(define input (input->List port read-line))
    768767
    769768(run
    770769  (= (Length (First-five)) 5)
    771770  (= (Length (Rest (First-five))) 4)
    772   (->> (Cardinals) (Rest) (Length) (eq? #f))
    773   (->> (Cardinals) (Take 5) (Length) (= 5))
    774   (->> (Cardinals) (Length) (eq? #f))
    775   (->> (Cardinals) (Drop 5) (Length) (eq? #f))
    776   (->> (Cardinals) (Drop 5) (First) (= 5))
    777   (->> (First-five) (List->list) (equal? '(0 1 2 3 4)))
    778   (->> (Cardinals) (Take 5) (List->list) (equal? '(0 1 2 3 4)))
    779   (->> (Interval 2 10) (Length) (= (- 10 2)))
    780   (->> (Interval 2 10) (List->list) (equal? '(2 3 4 5 6 7 8 9)))
    781   (->> (Interval 10 2) (List->list) (equal? '(10 9 8 7 6 5 4 3)))
     771  (eq? (Length (Rest (Cardinals))) #f)
     772  (= (Length (Take 5 (Cardinals))) 5)
     773  (eq? (Length (Cardinals)) #f)
     774  (eq? (Length (Drop 5 (Cardinals))) #f)
     775  (= (First (Drop 5 (Cardinals))) 5)
     776  (equal? (List->list (First-five)) '(0 1 2 3 4))
     777  (equal? (List->list (Take 5 (Cardinals))) '(0 1 2 3 4))
     778  (= (Length (Interval 2 10)) (- 10 2))
     779  (equal? (List->list (Interval 2 10)) '(2 3 4 5 6 7 8 9))
     780  (equal? (List->list (Interval 10 2)) '(10 9 8 7 6 5 4 3))
    782781  (equal?
    783782    (receive (head index tail) (Split-with (cut = <> 3) (First-five))
     
    788787      (append (List->list tail) (List->list head)))
    789788    '(5 6 7 8 9 0 1 2 3 4))
    790   (->> (First-five) (Index (cut = <> 2)) (= 2))
    791   (->> (First-five) (Index (cut = <> 20)) (= 5))
    792   (->> (Cardinals) (Take 10) (Take-upto (cut = <> 5)) (List->list)
    793        (equal? '(0 1 2 3 4)))
    794   (->> (Cardinals) (Take 10) (Take-upto (cut = <> 5)) (Length) (= 5))
    795   (->> (Cardinals) (Take 10) (Drop-upto (cut = <> 5)) (Length) (= 5))
    796   (->> (Cardinals) (Take 10) (Drop-upto (cut = <> 5)) (First) (= 5))
    797   (->> (First-five) (Drop-upto (cut = <> 2)) (Length) (= 3))
    798   (->> (First-five) (Drop-upto (cut = <> 2)) (First) (= 2))
    799   (->> (First-five) (Memp odd?) (List->list) (equal? '(1 2 3 4)))
    800   (->> (Cardinals) (Take 10) (Memv 5) (List->list) (equal? '(5 6 7 8 9)))
    801   (->> (Cardinals) (Map (lambda (x) (list x x))) (Take 10) (Assv 5)
    802        (equal? '(5 5)))
    803   (->> (First-five) (Map (lambda (x) (list x x))) (Assv 10)
    804        (eq? #f))
    805   (->> (Cardinals) (Equal? (Cardinals)) (eq? #f))
    806   (->> (Cardinals) (Equal? (First-five)) (eq? #f))
    807   (->> (First-five) (Equal? (First-five)) (eq? #t))
    808   (->> (Cardinals) (Take 10) (Length) (= 10))
    809   (->> (Cardinals) (Drop 1) (Filter odd?) (Take 5) (List->list)
    810        (equal? '(1 3 5 7 9)))
    811   (->> (Cardinals) (Length) (eq? #f))
    812   (->> (First-five) (Map add1) (List->list) (equal? '(1 2 3 4 5)))
    813   (->> (Map + (First-five) (Take 5 (Cardinals))) (List->list)
    814        (equal? '(0 2 4 6 8)))
    815   (->> (Map + (Cardinals) (Cardinals)) (Length) (eq? #f))
    816   (->> (Filter odd? (First-five)) (Length) (= 2))
    817   (->> (Filter odd? (First-five)) (List->list) (equal? '(1 3)))
    818   (->> (Filter odd? (Cardinals)) (Length) (eq? #f))
    819   (->> (Zip (Cardinals) (Cardinals)) (Sieve =) (Ref 20) (= 20))
    820   (->> (Zip (First-five) (First-five)) (Sieve =) (List->list)
    821        (equal? '(0 1 2 3 4)))
    822   (->> (Ref 25 (Cardinals)) (= 25))
    823   (->> (Ref 2 (First-five)) (= 2))
    824   (->> (Repeat #f) (Take 3) (List->list) (equal? '(#f #f #f)))
    825   (->> (Repeatedly (lambda () 1))(Take 3)  (List->list)
    826        (equal? '(1 1 1)))
    827   (->> (Iterate add1 0) (Take 3) (List->list) (equal? '(0 1 2)))
    828   (->> (Iterate add1 0) (Length) (eq? #f))
    829   (->> (Append (First-five) (First-five)) (Length) (= 10))
    830   (->> (Append (First-five) (First-five)) (List->list)
    831        (equal? '(0 1 2 3 4 0 1 2 3 4)))
    832   (->> (Append (First-five) (Cardinals)) (Take 12) (List->list)
    833        (equal? '(0 1 2 3 4 0 1 2 3 4 5 6)))
    834   (->> (Append (First-five) (Cardinals)) (Length) (eq? #f))
    835   (->> (First-five) (Reverse) (List->list) (equal? '(4 3 2 1 0)))
    836   (->> (Cardinals) (Take 5) (Reverse) (List->list) (equal? '(4 3 2 1 0)))
    837   (->> (First-five) (Reverse) (Length) (= 5))
    838   (->> (Cardinals) (Reverse*) (Length) (eq? #f))
    839   (->> (Cardinals) (Reverse*) (Ref 5) (List->list) (equal? '(5 4 3 2 1 0)))
    840   (->> (Cycle (First-five)) (Take 10) (List->list)
    841        (equal? '(0 1 2 3 4 0 1 2 3 4)))
    842   (->> (Cycle (First-five)) (Length) (eq? #f))
    843   (->> (Sort < (First-five)) (List->list) (equal? '(0 1 2 3 4)))
    844   (->> (Sort < (First-five)) (Length) (= 5))
    845   (->> (Sort < (List 3 1 0 2 4)) (List->list) (equal? '(0 1 2 3 4)))
     789  (= (Index (cut = <> 2) (First-five)) 2)
     790  (= (Index (cut = <> 20) (First-five)) 5)
     791  (equal? (List->list (Take-upto (cut = <> 5) (Take 10 (Cardinals))))
     792       '(0 1 2 3 4))
     793  (= (Length (Take-upto (cut = <> 5) (Take 10 (Cardinals)))) 5)
     794  (= (Length (Drop-upto (cut = <> 5) (Take 10 (Cardinals)))) 5)
     795  (= (First (Drop-upto (cut = <> 5) (Take 10 (Cardinals)))) 5)
     796  (= (Length (Drop-upto (cut = <> 2) (First-five))) 3)
     797  (= (First (Drop-upto (cut = <> 2) (First-five))) 2)
     798  (equal? (List->list (Memp odd? (First-five))) '(1 2 3 4))
     799  (equal? (List->list (Memv 5 (Take 10 (Cardinals)))) '(5 6 7 8 9))
     800  (equal? (Assv 5 (Take 10 (Map (lambda (x) (list x x)) (Cardinals))))
     801    '(5 5))
     802  (eq? (Assv 10 (Map (lambda (x) (list x x)) (First-five))) #f)
     803  (eq? (Equal? (Cardinals) (Cardinals)) #f)
     804  (eq? (Equal? (Cardinals) (First-five)) #f)
     805  (eq? (Equal? (First-five) (First-five)) #t)
     806  (= (Length (Take 10 (Cardinals))) 10)
     807  (equal? (List->list (Take 5 (Filter odd? (Drop 1 (Cardinals)))))
     808    '(1 3 5 7 9))
     809  (eq? (Length (Cardinals)) #f)
     810  (equal? (List->list (Map add1 (First-five))) '(1 2 3 4 5))
     811  (equal? (List->list (Map + (First-five) (Take 5 (Cardinals))))
     812    '(0 2 4 6 8))
     813  (eq? (Length (Map + (Cardinals) (Cardinals))) #f)
     814  (= (Length (Filter odd? (First-five))) 2)
     815  (equal? (List->list (Filter odd? (First-five))) '(1 3))
     816  (eq? (Length (Filter odd? (Cardinals))) #f)
     817  (= (Ref 20 (Sieve = (Zip (Cardinals) (Cardinals)))) 20)
     818  (equal? (List->list (Sieve = (Zip (First-five) (First-five))))
     819    '(0 1 2 3 4))
     820  (= (Ref 25 (Cardinals)) 25)
     821  (= (Ref 2 (First-five)) 2)
     822  (equal? (List->list (Take 3 (Repeat #f))) '(#f #f #f))
     823  (equal? (List->list (Take 3 (Repeatedly (lambda () 1))))
     824    '(1 1 1))
     825  (equal? (List->list (Take 3 (Iterate add1 0))) '(0 1 2))
     826  (eq? (Length (Iterate add1 0)) #f)
     827  (= (Length (Append (First-five) (First-five))) 10)
     828  (equal? (List->list  (Append (First-five) (First-five)))
     829    '(0 1 2 3 4 0 1 2 3 4))
     830  (equal? (List->list (Take 12 (Append (First-five) (Cardinals))))
     831    '(0 1 2 3 4 0 1 2 3 4 5 6))
     832  (eq? (Length (Append (First-five) (Cardinals))) #f)
     833  (equal? (List->list (Reverse (First-five))) '(4 3 2 1 0))
     834  (equal? (List->list (Reverse (Take 5 (Cardinals)))) '(4 3 2 1 0))
     835  (= (Length (Reverse (First-five))) 5)
     836  (eq? (Length (Reverse* (Cardinals))) #f)
     837  (equal? (List->list (Ref 5 (Reverse* (Cardinals)))) '(5 4 3 2 1 0))
     838  (equal? (List->list (Take 10 (Cycle (First-five))))
     839    '(0 1 2 3 4 0 1 2 3 4))
     840  (eq? (Length (Cycle (First-five))) #f)
     841  (equal? (List->list (Sort < (First-five))) '(0 1 2 3 4))
     842  (= (Length (Sort < (First-five))) 5)
     843  (equal? (List->list (Sort < (List 3 1 0 2 4))) '(0 1 2 3 4))
    846844  (equal?
    847845    (receive (head tail) (Split-at 5 (Cardinals))
     
    852850      (append (List->list tail) (List->list head)))
    853851    '(0 1 2 3 4))
    854   (->> (Cardinals) (Take 5) (Fold-left + 0) (= 10))
    855   (->> (Fold-left + 0 (First-five) (First-five)) (= 20))
    856   (->> (List 1 2 3 4) (Fold-left * 1) (= 24))
    857   (->> (Cardinals) (Take 5) (Fold-left cons '())
    858        (equal? '(((((() . 0) . 1) . 2) . 3) . 4)))
    859   (->> (Cardinals) (Fold-left* cons '()) (Ref 4)
    860        (equal? '(((((() . 0) . 1) . 2) . 3) . 4)))
    861   (->> (Cardinals) (Take 5) (Fold-right + 0) (= 10))
    862   (->> (Fold-right + 0 (First-five) (First-five)) (= 20))
    863   (->> (First-five) (Fold-right cons '())
    864        (equal? '(0 1 2 3 4))) ; list
    865   (->> (First-five) (Fold-right cons '(a b c))
    866        (equal? '(0 1 2 3 4 a b c))) ; append
    867   (->> (Cardinals) (Fold-right* cons '()) (Ref 4)
    868        (equal? '(4 3 2 1 0))) ; note changed order
    869   (->> (Cardinals) (Fold-right* cons-right '()) (Ref 4)
    870        (equal? '(0 1 2 3 4)))
    871   (->> (Cardinals) (Fold-right* cons '(a b c)) (Ref 4)
    872        (equal? '(4 3 2 1 0 a b c))) ; note changed order
    873   (->> (Cardinals) (Fold-right* cons-right '(a b c)) (Ref 4)
    874        (equal? '(a b c 0 1 2 3 4)))
    875   (->> (vector->List '#(0 1 2 3 4)) (List->list) (equal? '(0 1 2 3 4)))
    876   (->> (vector->List '#()) (Null?))
    877   (->> (Take 5 (Cardinals)) (List->vector) (equal? '#(0 1 2 3 4)))
    878   (->> (List->vector (First-five)) (equal? '#(0 1 2 3 4)))
    879   (->> (List->vector Nil) (equal? '#()))
    880   (->> (Filter odd? (Cardinals)) (Take 15) (Every? odd?)
    881        (eq? #t))
    882   (->> (Take 15 (Cardinals)) (Every? odd?)
    883        (eq? #f))
    884   (->> (Every? odd? Nil) (eq? #t))
    885   (->> (Some? odd? Nil) (eq? #f))
    886   (->> (Filter even? (Cardinals)) (Take 5) (Some? odd?)
    887        (eq? #f))
    888   (->> (Some? odd? (First-five)) (eq? #t))
    889   (->> (Zip (Cardinals) (First-five)) (Length) (eq? #f))
    890   (->> (Zip (First-five) (Cardinals)) (Length) (eq? #f))
    891   (->> (Zip (Cardinals) (Cardinals)) (Length) (eq? #f))
    892   (->> (Zip (Cardinals) (First-five)) (Take 14)
    893        (Eqv? (List 0 0 1 1 2 2 3 3 4 4 5 6 7 8)))
    894   (->> (Zip (Cardinals) (Cardinals)) (Take 14)
    895        (Eqv? (List 0 0 1 1 2 2 3 3 4 4 5 5 6 6)))
    896   (->> (Zip (First-five) (First-five)) (Length) (= 10))
    897   (->> (Zip (First-five) (First-five))
    898        (Eqv? (List 0 0 1 1 2 2 3 3 4 4)))
    899   (->> (Primes) (Ref 50) (= 233))
    900   (->> (Primes) (Take 5) (Eqv? (List 2 3 5 7 11)))
    901   (->> (Fibs) (Take 10) (Eqv? (List  0 1 1 2 3 5 8 13 21 34)))
     852  (= (Fold-left + 0 (Take 5 (Cardinals))) 10)
     853  (= (Fold-left + 0 (First-five) (First-five)) 20)
     854  (= (Fold-left * 1 (List 1 2 3 4)) 24)
     855  (equal? (Fold-left cons '() (Take 5 (Cardinals)))
     856    '(((((() . 0) . 1) . 2) . 3) . 4))
     857  (equal? (Ref 4 (Fold-left* cons '() (Cardinals)))
     858    '(((((() . 0) . 1) . 2) . 3) . 4))
     859  (= (Fold-right + 0 (Take 5 (Cardinals))) 10)
     860  (= (Fold-right + 0 (First-five) (First-five)) 20)
     861  (equal? (Fold-right cons '() (First-five))
     862    '(0 1 2 3 4)) ; list
     863  (equal? (Fold-right cons '(a b c) (First-five))
     864    '(0 1 2 3 4 a b c)) ; append
     865  (equal? (Ref 4 (Fold-right* cons '() (Cardinals)))
     866    '(4 3 2 1 0)) ; note changed order
     867  (equal? (Ref 4 (Fold-right* cons-right '() (Cardinals)))
     868    '(0 1 2 3 4))
     869  (equal? (Ref 4 (Fold-right* cons '(a b c) (Cardinals)))
     870    '(4 3 2 1 0 a b c)) ; note changed order
     871  (equal? (Ref 4 (Fold-right* cons-right '(a b c) (Cardinals)))
     872    '(a b c 0 1 2 3 4))
     873  (equal? (List->list (vector->List '#(0 1 2 3 4))) '(0 1 2 3 4))
     874  (Null? (vector->List '#()))
     875  (equal? (List->vector (Take 5 (Cardinals))) '#(0 1 2 3 4))
     876  (equal? (List->vector (First-five)) '#(0 1 2 3 4))
     877  (equal? (List->vector Nil) '#())
     878  (eq? (Every? odd? (Take 15 (Filter odd? (Cardinals)))) #t)
     879  (eq? (Every? odd? (Take 15 (Cardinals))) #f)
     880  (eq? (Every? odd? Nil) #t)
     881  (eq? (Some? odd? Nil) #f)
     882  (eq? (Some? odd? (Take 5 (Filter even? (Cardinals)))) #f)
     883  (eq? (Some? odd? (First-five)) #t)
     884  (eq? (Length (Zip (Cardinals) (First-five))) #f)
     885  (eq? (Length (Zip (First-five) (Cardinals))) #f)
     886  (eq? (Length (Zip (Cardinals) (Cardinals))) #f)
     887  (= (Length (Zip (First-five) (First-five))) 10)
     888  (Eqv? (Take 14 (Zip (Cardinals) (First-five)))
     889    (List 0 0 1 1 2 2 3 3 4 4 5 6 7 8))
     890  (Eqv? (Take 14 (Zip (Cardinals) (Cardinals)))
     891    (List 0 0 1 1 2 2 3 3 4 4 5 5 6 6))
     892  (Eqv? (Zip (First-five) (First-five))
     893    (List 0 0 1 1 2 2 3 3 4 4))
     894  (= (Ref 50 (Primes)) 233)
     895  (Eqv? (Take 5 (Primes)) (List 2 3 5 7 11))
     896  (Eqv? (Take 10 (Fibs)) (List  0 1 1 2 3 5 8 13 21 34))
    902897  ;; compute square root
    903898  (let ((eps 0.000001))
    904899    (< (abs (- (sqrt 2) (Within eps (Iterate (Newton 2) 2)))) eps))
    905   (->> (First-five) (Sums) (List->list) (equal? '(0 1 3 6 10)))
     900  (equal? (List->list (Sums (First-five))) '(0 1 3 6 10))
    906901  )
    907902
     
    918913== Updated
    919914
    920 Aug 2, 2012
     915Aug 5, 2012
    921916
    922917== License
     
    935930== Version History
    936931
    937  0.1 : initial import
    938 
     932; 0.2 : dependency on clojurian removed
     933; 0.1 : initial import
     934
Note: See TracChangeset for help on using the changeset viewer.