cs7100(prasad)l11clos1 closures and streams. contemporary interest in closures the concept of...
TRANSCRIPT
![Page 1: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/1.jpg)
cs7100(Prasad) L11Clos 1
Closures and Streams
![Page 2: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/2.jpg)
Contemporary Interest in Closures
• The concept of closures was developed in the 1960s and was first fully implemented in 1975 as a language feature in the Scheme programming language to support lexically scoped first-class functions.
• Project Lambda makes it easier to write code for multi-core processors by adding closures to the Java language and extending the Java API to support parallelizable operations upon streamed data.
• Rick Hickey’s Clojure (a dialect of LISP for Java platform) is a pure functional language with support for rich set of data structures, and constructs for concurrent programming.
cs7100(Prasad) L11Clos 2
![Page 3: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/3.jpg)
cs7100(Prasad) L11Clos 3
Models of Evaluation
Substitution-based (define (square x) (* x x))
((lambda (x y) (+ (square x) (square y))) (- 5 3) 5)
= (+ (square 2) (square 5)) = (+ (* 2 2) (* 5 5))= (+ 4 25)
= 29
![Page 4: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/4.jpg)
cs7100(Prasad) L11Clos 4
Expression Evaluation Options
To evaluate: (operator operand1 operand2 operand3 ...)
• Applicative-Order Evaluation (call by value)
– evaluate each of the sub-expressions.
– apply the leftmost result to the rest.
• Normal-Order Evaluation (call by name)– apply the leftmost (lambda) sub-expression to
the rest and expand. (Argument sub-expressions get evaluated when necessary.)
![Page 5: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/5.jpg)
cs7100(Prasad) L11Clos 5
Models of Evaluation
Environment-based
((lambda (x y) (+ (square x) (square y))) (- 5 3) 5)
= (+ (square x) (square y)) x=2,y=5= (+ (* x x) x=2,y=5 (* x xx x) ) xx=5,y=5= (+ 4 25)
= 29
![Page 6: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/6.jpg)
cs7100(Prasad) L11Clos 6
An extended example
(define square (lambda (x) (* x x)))
(define sum-of-squares (lambda (x y) (+ (square x) (square y))))
(define f (lambda (a) (sum-of-squares (+ a 1) (* a 2))))
![Page 7: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/7.jpg)
cs7100(Prasad) L11Clos 7
Initial Global Environment
![Page 8: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/8.jpg)
cs7100(Prasad) L11Clos 8
Executing (f 5) and (sum-of-squares 6 10)
![Page 9: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/9.jpg)
cs7100(Prasad) L11Clos 9
Delayed Evaluation : THUNKS
> (define x (* 5 5))> x 25
> (define y (lambda () (* 5 5))> (y) 25
Partial Evaluation : CURRYING
> (define add (lambda (x) (lambda (y) (+ x y)))
> (define ad4 (add 4))
> (ad4 8) 12
![Page 10: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/10.jpg)
cs7100(Prasad) L11Clos 10
Closure and Models• Substitution
(lambda (y) (+ 4 y))
Substitution model is inadequate for mutable data structures.
• Environment
< (lambda (y) (+ x y)) ,
[x <- 4] >
Need to distinguish location and contents of the location.
![Page 11: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/11.jpg)
cs7100(Prasad) L11Clos 11
Modular Designs with Lists
![Page 12: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/12.jpg)
cs7100(Prasad) L11Clos 12
Higher-order functions and lists
• Use of lists and generic higher-order functions enable abstraction and reuse– Can replace customized recursive definitions
with more readable definitions built using “library” functions
– The HOF approach may be less efficient.– Promotes MODULAR DESIGNS – improves
programmer productivity
![Page 13: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/13.jpg)
cs7100(Prasad) L11Clos 13
(define (even-fibs n) (define (next k) (if (> k n) ’() (let ((f (fib k))) (if (even? f) (cons f (next (+ k 1))) (next (+ k 1)) )) )) (next 0))
• Take a number n and construct a list of first n even Fibonacci numbers.
![Page 14: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/14.jpg)
cs7100(Prasad) L11Clos 14
Abstract Description
• enumerate integers from 0 to n• compute the Fibonacci number for
each integer
• filter them, selecting even ones
• accumulate the results using cons, starting with ()
![Page 15: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/15.jpg)
cs7100(Prasad) L11Clos 15
(define (filter pred seq) (cond ((null? seq) ’()) ((pred (car seq)) (cons (car seq) (filter pred (cdr
seq)))) (else (filter pred (cdr seq)))))
(define (accumulate op init seq) (if (null? seq) init (op (car seq) (accumulate op init (cdr
seq)))))
![Page 16: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/16.jpg)
cs7100(Prasad) L11Clos 16
(define (enum-interval low high) (if (> low high) ’() (cons low (enum-interval (+ low 1) high))
))
(define (even-fibs n) (accumulate cons ’() (filter even? (map fib (enum-interval 0 n)))))
![Page 17: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/17.jpg)
cs7100(Prasad) L11Clos 17
Streams: Motivation
![Page 18: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/18.jpg)
cs7100(Prasad) L11Clos 18
• Modeling real-world objects (with state) and real-
world phenomena
– Use computational objects with local variables and
implement time variation of states using assignments
– Alternatively, use sequences to model time histories of
the states of the objects.
• Possible Implementations of Sequences
– Using Lists
– Using Streams
• Delayed evaluation (demand-based evaluation) useful
(necessary) when large (infinite) sequences are considered.
![Page 19: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/19.jpg)
cs7100(Prasad) L11Clos 19
Streams : Equational Reasoning
(define s (cons 0 s)) Illegal. (Solution: infinite sequence of 0’s.)
(0 . (0. (0. (0. … … )))) (cf. Ada, Pascal,…) type s = record car : integer; cdr : s end;
How do we represent potentially infinite structures?
![Page 20: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/20.jpg)
cs7100(Prasad) L11Clos 20
(0.(0.(0. … )))
(0. Function which when executed generates
an infinite structure )Recursive winding and unwinding
(0. )
(0. ) (0. . . . )
![Page 21: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/21.jpg)
cs7100(Prasad) L11Clos 21
>(define stream-car car)
>(define (stream-cdr s) ( ( (cdr s) )) )
• Unwrap by executing the second.
>(define stream-zeros (cons 0 (lambda()(lambda()
stream-zeros)) ) )• Wrap by forming closure (thunk).
![Page 22: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/22.jpg)
cs7100(Prasad) L11Clos 23
>(stream-car (stream-cdr stream-zeros) )
>(define (numbers-from n) (cons n (lambda () (numbers-from (+ 1 n)) ))) >(define stream-numbers (numbers-from 0) )
![Page 23: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/23.jpg)
cs7100(Prasad) L11Clos 24
(define stream-car car)
(define (stream-cdr s)
(( (cdr s) )) )
(define (stream-cons x s)
(cons x ( lambda ( )( lambda ( ) s)) ) )
(define the-empty-stream ’() )
(define stream-null? null?)
Recapitulating Stream Primitives
![Page 24: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/24.jpg)
cs7100(Prasad) L11Clos 25
(define (stream-filter p s) (cond ((stream-null? s) the-empty-stream) ((p (stream-car s)) (stream-cons (stream-car s) (stream-filter p (stream-cdr
s)))) (else (stream-filter p (stream-cdr s)))))
(define (stream-enum-interval low high) (if (> low high) the-empty-stream (stream-cons low (stream-enum-interval (+ 1 low)
high))))
![Page 25: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/25.jpg)
cs7100(Prasad) L11Clos 26
(stream-car (stream-cdr (stream-filter prime? (stream-enum-interval 100
1000))))
(define (fibgen f1 f2)
(cons f1 (lambda () (fibgen f2 (+ f1 f2)))
))
(define fibs (fibgen 0 1))
![Page 26: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/26.jpg)
cs7100(Prasad) L11Clos 27
Factorial Revisited
(define (trfac n)
(letrec
( (iter (lambda (i a)
(if (zero? i) a
(iter (- i 1) (* a i)))))
)
(iter n 1)
)
)
![Page 27: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/27.jpg)
cs7100(Prasad) L11Clos 28
(define (ifac n) (let (( i n ) ( a 1 )) (letrec ( (iter (lambda () (if (zero? i) a (begin (set! a (* a i)) (set! i (- i 1)) (iter) )) ) ) ) (iter) )))
![Page 28: Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully](https://reader036.vdocuments.us/reader036/viewer/2022062714/56649d365503460f94a0ea8a/html5/thumbnails/28.jpg)
cs7100(Prasad) L11Clos 29
Factorial Stream(define (str n r)
(cons r (lambda ()
(str (+ n 1) (* n r)) ) ) )(define sfac (str 1 1))
(car ((cdr ((cdr ((cdr sfac)) )) )) )… (stream-cdr … )
• Demand driven generation of list elements.
• Caching/Memoing necessary for efficiency.
• Avoids assignment.