Changeset 29260 in project
 Timestamp:
 06/27/13 11:35:17 (8 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

wiki/eggref/4/lazylists
r29252 r29260 464 464 <enscript highlight=scheme> 465 465 466 (requirelibrary lazylists simpletests)467 (import lazylists )466 (requirelibrary lazylists multimethods) 467 (import lazylists methods) 468 468 469 469 (define (consright var lst) … … 497 497 (Append (List 0 1) (Lazy #f (Map + (Rest (Fibs)) (Fibs))))) 498 498 499 ;(define port (openinputfile "lazylists.scm")) 500 ;(define input (input>List port readline)) 501 502 (runtests 503 (= (Length (Firstfive)) 5) 504 (= (Length (Rest (Firstfive))) 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 (Firstfive)) '(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) (Splitwith (cut = <> 3) (Firstfive)) 517 (cons (First tail) (List>list head))) 518 '(3 0 1 2)) 519 (equal? 520 (receive (head index tail) (Splitwith (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) (Firstfive)) 2) 524 (= (Index (cut = <> 20) (Firstfive)) 5) 525 (equal? (List>list (Takeupto (cut = <> 5) (Take 10 (Cardinals)))) 526 '(0 1 2 3 4)) 527 (= (Length (Takeupto (cut = <> 5) (Take 10 (Cardinals)))) 5) 528 (= (Length (Dropupto (cut = <> 5) (Take 10 (Cardinals)))) 5) 529 (= (First (Dropupto (cut = <> 5) (Take 10 (Cardinals)))) 5) 530 (= (Length (Dropupto (cut = <> 2) (Firstfive))) 3) 531 (= (First (Dropupto (cut = <> 2) (Firstfive))) 2) 532 (equal? (List>list (Memp odd? (Firstfive))) '(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)) (Firstfive))) #f) 537 (eq? (Equal? (Cardinals) (Cardinals)) #f) 538 (eq? (Equal? (Cardinals) (Firstfive)) #f) 539 (eq? (Equal? (Firstfive) (Firstfive)) #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 (Firstfive))) '(1 2 3 4 5)) 545 (equal? (List>list (Map + (Firstfive) (Take 5 (Cardinals)))) 546 '(0 2 4 6 8)) 547 (eq? (Length (Map + (Cardinals) (Cardinals))) #f) 548 (= (Length (Filter odd? (Firstfive))) 2) 549 (equal? (List>list (Filter odd? (Firstfive))) '(1 3)) 550 (eq? (Length (Filter odd? (Cardinals))) #f) 551 (= (Ref 20 (Sieve = (Zip (Cardinals) (Cardinals)))) 20) 552 (equal? (List>list (Sieve = (Zip (Firstfive) (Firstfive)))) 553 '(0 1 2 3 4)) 554 (= (Ref 25 (Cardinals)) 25) 555 (= (Ref 2 (Firstfive)) 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 (Firstfive) (Firstfive))) 10) 562 (equal? (List>list (Append (Firstfive) (Firstfive))) 563 '(0 1 2 3 4 0 1 2 3 4)) 564 (equal? (List>list (Take 12 (Append (Firstfive) (Cardinals)))) 565 '(0 1 2 3 4 0 1 2 3 4 5 6)) 566 (eq? (Length (Append (Firstfive) (Cardinals))) #f) 567 (equal? (List>list (Reverse (Firstfive))) '(4 3 2 1 0)) 568 (equal? (List>list (Reverse (Take 5 (Cardinals)))) '(4 3 2 1 0)) 569 (= (Length (Reverse (Firstfive))) 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 (Firstfive)))) 573 '(0 1 2 3 4 0 1 2 3 4)) 574 (eq? (Length (Cycle (Firstfive))) #f) 575 (equal? (List>list (Sort < (Firstfive))) '(0 1 2 3 4)) 576 (= (Length (Sort < (Firstfive))) 5) 577 (equal? (List>list (Sort < (List 3 1 0 2 4))) '(0 1 2 3 4)) 578 (equal? 579 (receive (head tail) (Splitat 5 (Cardinals)) 580 (cons (First tail) (List>list head))) 581 '(5 0 1 2 3 4)) 582 (equal? 583 (receive (head tail) (Splitat 15 (Take 5 (Cardinals))) 584 (append (List>list tail) (List>list head))) 585 '(0 1 2 3 4)) 586 (= (Foldleft + 0 (Take 5 (Cardinals))) 10) 587 (= (Foldleft + 0 (Firstfive) (Firstfive)) 20) 588 (= (Foldleft * 1 (List 1 2 3 4)) 24) 589 (equal? (Foldleft cons '() (Take 5 (Cardinals))) 590 '(((((() . 0) . 1) . 2) . 3) . 4)) 591 (equal? (Ref 4 (Foldleft* cons '() (Cardinals))) 592 '(((((() . 0) . 1) . 2) . 3) . 4)) 593 (= (Foldright + 0 (Take 5 (Cardinals))) 10) 594 (= (Foldright + 0 (Firstfive) (Firstfive)) 20) 595 (equal? (Foldright cons '() (Firstfive)) 596 '(0 1 2 3 4)) ; list 597 (equal? (Foldright cons '(a b c) (Firstfive)) 598 '(0 1 2 3 4 a b c)) ; append 599 (equal? (Ref 4 (Foldright* cons '() (Cardinals))) 600 '(4 3 2 1 0)) ; note changed order 601 (equal? (Ref 4 (Foldright* consright '() (Cardinals))) 602 '(0 1 2 3 4)) 603 (equal? (Ref 4 (Foldright* cons '(a b c) (Cardinals))) 604 '(4 3 2 1 0 a b c)) ; note changed order 605 (equal? (Ref 4 (Foldright* consright '(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 (Firstfive)) '#(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? (Firstfive)) #t) 618 (eq? (Length (Zip (Cardinals) (Firstfive))) #f) 619 (eq? (Length (Zip (Firstfive) (Cardinals))) #f) 620 (eq? (Length (Zip (Cardinals) (Cardinals))) #f) 621 (= (Length (Zip (Firstfive) (Firstfive))) 10) 622 (Eqv? (Take 14 (Zip (Cardinals) (Firstfive))) 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 (Firstfive) (Firstfive)) 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 (Firstfive))) '(0 1 3 6 10)) 635 ) 499 ;; some tests 500 (Length (Firstfive)) ;> 5 501 (Length (Rest (Firstfive))) ;> 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 (Firstfive)) ;> '(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) (Splitwith (cut = <> 3) (Firstfive)) 513 (cons (First tail) (List>list head))) ;> '(3 0 1 2) 514 (receive (head index tail) (Splitwith (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) (Firstfive)) ;> 2 517 (Index (cut = <> 20) (Firstfive)) ;> 5 518 (List>list (Takeupto (cut = <> 5) (Take 10 (Cardinals)))) 519 ;> '(0 1 2 3 4) 520 (Length (Takeupto (cut = <> 5) (Take 10 (Cardinals)))) ;> 5 521 (Length (Dropupto (cut = <> 5) (Take 10 (Cardinals)))) ;> 5 522 (First (Dropupto (cut = <> 5) (Take 10 (Cardinals)))) ;> 5 523 (Length (Dropupto (cut = <> 2) (Firstfive))) ;> 3 524 (First (Dropupto (cut = <> 2) (Firstfive))) ;> 2 525 (List>list (Memp odd? (Firstfive))) ;> '(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)) (Firstfive))) ;> #f 530 (Equal? (Cardinals) (Cardinals)) ;> #f 531 (Equal? (Cardinals) (Firstfive)) ;> #f 532 (Equal? (Firstfive) (Firstfive)) ;> #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 (Firstfive))) ;> '(1 2 3 4 5) 538 (List>list (Map + (Firstfive) (Take 5 (Cardinals)))) 539 ;> '(0 2 4 6 8) 540 (Length (Map + (Cardinals) (Cardinals))) ;> #f 541 (Length (Filter odd? (Firstfive))) ;> 2 542 (List>list (Filter odd? (Firstfive))) ;> '(1 3) 543 (Length (Filter odd? (Cardinals))) ;> #f 544 (Ref 20 (Sieve = (Zip (Cardinals) (Cardinals)))) ;> 20 545 (List>list (Sieve = (Zip (Firstfive) (Firstfive)))) 546 ;> '(0 1 2 3 4) 547 (Ref 25 (Cardinals)) ;> 25 548 (Ref 2 (Firstfive)) ;> 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 (Firstfive) (Firstfive))) ;> 10 555 (List>list (Append (Firstfive) (Firstfive))) 556 ;> '(0 1 2 3 4 0 1 2 3 4) 557 (List>list (Take 12 (Append (Firstfive) (Cardinals)))) 558 ; > '(0 1 2 3 4 0 1 2 3 4 5 6) 559 (Length (Append (Firstfive) (Cardinals))) ; > #f 560 (List>list (Reverse (Firstfive))) ; > '(4 3 2 1 0) 561 (List>list (Reverse (Take 5 (Cardinals)))) ; > '(4 3 2 1 0) 562 (Length (Reverse (Firstfive))) ; > 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 (Firstfive)))) 566 ; > '(0 1 2 3 4 0 1 2 3 4) 567 (Length (Cycle (Firstfive))) ; > #f 568 (List>list (Sort < (Firstfive))) ; > '(0 1 2 3 4) 569 (Length (Sort < (Firstfive))) ; > 5 570 (List>list (Sort < (List 3 1 0 2 4))) ; > '(0 1 2 3 4) 571 (receive (head tail) (Splitat 5 (Cardinals)) 572 (cons (First tail) (List>list head))) 573 ; > '(5 0 1 2 3 4) 574 (receive (head tail) (Splitat 15 (Take 5 (Cardinals))) 575 (append (List>list tail) (List>list head))) 576 ; > '(0 1 2 3 4) 577 (Foldleft + 0 (Take 5 (Cardinals))) ; > 10 578 (Foldleft + 0 (Firstfive) (Firstfive)) ; > 20 579 (Foldleft * 1 (List 1 2 3 4)) ; > 24 580 (Foldleft cons '() (Take 5 (Cardinals))) 581 ; > '(((((() . 0) . 1) . 2) . 3) . 4) 582 (Ref 4 (Foldleft* cons '() (Cardinals))) 583 ; > '(((((() . 0) . 1) . 2) . 3) . 4) 584 (Foldright + 0 (Take 5 (Cardinals))) ; > 10 585 (Foldright + 0 (Firstfive) (Firstfive)) ; > 20 586 ;; list 587 (Foldright cons '() (Firstfive)) 588 ; > '(0 1 2 3 4) 589 ;; append 590 (Foldright cons '(a b c) (Firstfive)) 591 ; > '(0 1 2 3 4 a b c) 592 (Ref 4 (Foldright* cons '() (Cardinals))) 593 ; > '(4 3 2 1 0)) ; note changed order 594 (Ref 4 (Foldright* consright '() (Cardinals))) 595 ; > '(0 1 2 3 4) 596 (Ref 4 (Foldright* cons '(a b c) (Cardinals))) 597 ; > '(4 3 2 1 0 a b c) ; note changed order 598 (Ref 4 (Foldright* consright '(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 (Firstfive)) ; > '#(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? (Firstfive)) ; > #t 611 (Length (Zip (Cardinals) (Firstfive))) ; > #f 612 (Length (Zip (Firstfive) (Cardinals))) ; > #f 613 (Length (Zip (Cardinals) (Cardinals))) ; > #f 614 (Length (Zip (Firstfive) (Firstfive))) ; > 10 615 (Eqv? (Take 14 (Zip (Cardinals) (Firstfive))) 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 (Firstfive) (Firstfive)) 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 (Firstfive))) ; > '(0 1 3 6 10) 636 628 637 629 </enscript>
Note: See TracChangeset
for help on using the changeset viewer.