Presentation is loading. Please wait.

Presentation is loading. Please wait.

PPL Lazy Lists.

Similar presentations


Presentation on theme: "PPL Lazy Lists."— Presentation transcript:

1 PPL Lazy Lists

2

3 Midterm 2012

4

5

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

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

8

9 Lazy Lists (Streams) 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 e1 … en) (append l1 l2) (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 . #<procedure>) > ((cdr l1)) > l2 ’(2 . #<procedure>) > ((cdr l2))

13 Real-World Example The recursion has no base!
;; [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>) > ((cdr ints)) ’(1 . #<procedure>) > ((cdr ((cdr ints)))) ’(2 . #<procedure>) 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. #<procedure>) > (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) ‘( )

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

17

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) ’( ) > (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) ’( )

21 Fibonacci Lazy List (define fibs (letrec ((fibgen (lambda (a b) (cons a (lambda () (fibgen b (+ a b))))))) (fibgen 0 1))) > (take fibs 7) ’( )

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) ’( )

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) ’( )

26 Fibonacci Using Lazy List Addition
(define fib-numbers (cons 0 (lambda () 1 (lz-lst-add (tail fib-numbers) fib-numbers)))))) > (take fib-numbers 7) ’( )

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) ’( )

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)))))


Download ppt "PPL Lazy Lists."

Similar presentations


Ads by Google