Download presentation
Presentation is loading. Please wait.
Published byZoe Matthews Modified over 8 years ago
1
PPL Lazy Lists
3
Midterm 2012
6
(define sum-vals (λ (ts) (if (ts-simple? ts) (ts-val ts) (accumulate + 0 (map ts-val (ts-inner-slots ts))))))
7
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...
9
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!
10
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 e 1 … e n ) – (append l 1 l 2 ) – (map p lst)
11
Lazy List: Constructor (list) – the empty lazy list cons (same as pair and list) [T * [Empty -> Lazy-List(T)] -> Lazy-List(T)]
12
Simple Lazy List > (define l0 (list)) > (define l1 (cons 1 (lambda () l0))) > (define l2 (cons 2 (lambda () l1))) > l0 ’() > l1 ’(1. # ) > ((cdr l1)) ’() > l2 ’(2. # ) > ((cdr l2)) ’(1. # )
13
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. # ) > ((cdr ints)) ’(1. # ) > ((cdr ((cdr ints)))) ’(2. # ) The recursion has no base!
14
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) ((cdr lz-lst)))) > (head ints) 0 > (tail ints) (1. # ) > (head (tail ints)) 1 …
15
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)
16
The n th Element (define nth (lambda (lz-lst n) (if (= n 0) (head lz-lst) (nth (tail lz-lst) (sub1 n))))) >(nth ints 44) 44
18
Lazy List Lots of examples in following slides Tip: always look for the cons
19
Integer Lazy List (define ones (cons 1 (lambda () ones))) >(take ones 7) ’(1 1 1 1 1 1 1) > (nth ones 10) 1
20
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)
21
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)
22
Lazy List Processing If we want to manipulate a lazy-list, we need to construct another lazy-list Examples on next slides
23
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)
24
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))))))))
25
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)
26
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)
27
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)
28
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
29
Lazy List of Primes (define primes (cons 2 (λ () (lz-lst-filter prime? (integers-from 3)))))
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.