1 / 29

PPL

PPL. Lazy Lists. Midterm 2012. (define sum- vals ( λ ( ts ) (if ( ts -simple? ts ) ( ts-val ts ) (accumulate + 0 (map ts-val ( ts -inner-slots ts )))))). VERY Long Lists. (accumulate + 0 (enumerate-list 1 1000000))

rufus
Download Presentation

PPL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PPL Lazy Lists

  2. Midterm 2012

  3. (define sum-vals (λ (ts) (if (ts-simple? ts) (ts-valts) (accumulate + 0 (map ts-val (ts-inner-slots ts))))))

  4. VERY Long Lists (accumulate + 0 (enumerate-list 1 1000000)) We will need to create a (very) large list... If there was only a way not to...

  5. Lazy Lists • We need a new data type • Elements are not pre-computed • Can be infinite! • Implemented in a way that delays the computation • We use lambdas!

  6. Lazy List • In normal-order, all lists are lazy • In app-order, all lists are not lazy. All following are already evaluated: • (cons head tail) • (list e1 … en) • (append l1 l2) • (map p lst)

  7. Lazy List: Constructor • (list) – the empty lazy list • cons (same as pair and list) • [T * [Empty -> Lazy-List(T)] -> Lazy-List(T)]

  8. Simple Lazy List > (define l0 (list)) > (define l1 (cons 1 (lambda () l0))) > (define l2 (cons 2 (lambda () l1))) > l0 ’() > l1 ’(1 . #<procedure>) > ((cdr l1)) ’() > l2 ’(2 . #<procedure>) > ((cdr l2)) ’(1 . #<procedure>)

  9. Real-World Example ;; [Num -> PAIR(Num,[Empty -> Lazy-List]) (define integers-from (lambda (n) (cons n (lambda () (integers-from (add1 n)))))) > (define ints (integers-from 0)) > ints ’(0 . #<procedure>) > ((cdrints)) ’(1 . #<procedure>) > ((cdr ((cdrints)))) ’(2 . #<procedure>) The recursion has no base!

  10. Lazy Lists: Head and Tail ;Signature: head(lz-ist) ;Type: [PAIR(T1,[Empty -> Lazy-list]) -> T1 ] (define head car) ;Signature: tail(lz-ist) ;Type: [PAIR(T1,[Empty -> Lazy-list]) -> Lazy-list ] (define tail (lambda (lz-lst) ((cdrlz-lst)))) > (head ints) 0 > (tail ints) (1. #<procedure>) > (head (tail ints)) 1 …

  11. First n Elements (define take (lambda (lz-lst n) (if (= n 0) (list) (cons (car lz-lst) (take (tail lz-lst) (sub1 n)))))) > (take ints 3) ‘(0 1 2) > (take ints 0) ‘() > (take (integers-from 30) 7) ‘(30 31 32 33 34 35 36)

  12. The nth Element (define nth (lambda (lz-lst n) (if (= n 0) (head lz-lst) (nth (tail lz-lst) (sub1 n))))) >(nth ints 44) 44

  13. Lazy List • Lots of examples in following slides • Tip: always look for the cons

  14. Integer Lazy List (define ones (cons 1 (lambda () ones))) >(take ones 7) ’(1 1 1 1 1 1 1) > (nth ones 10) 1

  15. Factorial Lazy List (define facts-from (lambda (k) (letrec ((helper (lambda (n fact-n) (cons fact-n (lambda () (helper (add1 n) (* (add1 n) fact-n))))))) (helper k (fact k))))) (define facts-from-3 (facts-from 3)) > (take facts-from-3 6) ’(6 24 120 720 5040 40320)

  16. Fibonacci Lazy List (define fibs (letrec ((fibgen (lambda (a b) (cons a (lambda () (fibgen b (+ a b))))))) (fibgen 0 1))) > (take fibs 7) ’(0 1 1 2 3 5 8)

  17. Lazy List Processing • If we want to manipulate a lazy-list, we need to construct another lazy-list • Examples on next slides

  18. Applying Square on Lazy List (define squares (lambda (lz-lst) (if (empty? lz-lst) lz-lst (cons (let ((h (head lz-lst))) (* h h)) (lambda () (squares (tail lz-lst))))))) > (take (squares ints) 7) ’(0 1 4 9 16 25 36)

  19. Lazy List Add (define lz-lst-add (lambda (lz1 lz2) (cond ((empty? lz1) lz2) ((empty? lz2) lz1) (else (cons (+ (head lz1) (head lz2)) (lambda () (lz-lst-add (tail lz1) (tail lz2))))))))

  20. Defining Integers using Lazy List Addition Reminder: (define ones (cons 1 (lambda () ones))) (define integers (cons 0 (lambda () (lz-lst-add ones integers)))) > (take integers 7) ’(0 1 2 3 4 5 6)

  21. Fibonacci Using Lazy List Addition (define fib-numbers (cons 0 (lambda () (cons 1 (lambda () (lz-lst-add (tail fib-numbers) fib-numbers)))))) > (take fib-numbers 7) ’(0 1 1 2 3 5 8)

  22. Lazy List Map (define lz-lst-map (λ (f lz) (if (empty? lz) lz (cons (f (head lz)) (λ () (lz-lst-map f (tail lz))))))) > (take (lz-lst-map (lambda (x) (* x x)) ints) 5) ’(0 1 4 9 16)

  23. Lazy List Filter (define lz-lst-filter (λ (p lz) (cond ((empty? lz) lz) ((p (head lz)) (cons (head lz) (λ () (lz-lst-filter p (tail lz))))) (else (lz-lst-filter p (tail lz)))))) (define (divisible? x y) (= (remainder x y) 0)) (define no-sevens (lz-lst-filter (lambda (x) (not (divisible? x 7))) ints)) > (nth no-sevens 100) ;The 100th integer not divisible by 7: 117

  24. Lazy List of Primes (define primes (cons 2 (λ () (lz-lst-filter prime? (integers-from 3)))))

More Related