300 likes | 469 Views
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))
E N D
PPL Lazy Lists
(define sum-vals (λ (ts) (if (ts-simple? ts) (ts-valts) (accumulate + 0 (map ts-val (ts-inner-slots ts))))))
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...
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!
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)
Lazy List: Constructor • (list) – the empty lazy list • cons (same as pair and list) • [T * [Empty -> Lazy-List(T)] -> Lazy-List(T)]
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>)
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!
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 …
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)
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
Lazy List • Lots of examples in following slides • Tip: always look for the cons
Integer Lazy List (define ones (cons 1 (lambda () ones))) >(take ones 7) ’(1 1 1 1 1 1 1) > (nth ones 10) 1
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)
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)
Lazy List Processing • If we want to manipulate a lazy-list, we need to construct another lazy-list • Examples on next slides
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)
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))))))))
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)
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)
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)
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
Lazy List of Primes (define primes (cons 2 (λ () (lz-lst-filter prime? (integers-from 3)))))