Lecture 22-Streams
Lecture 22 - Streams
Streams
Streams
Lazy Evaluation in Scheme
Lazy Evaluation in Scheme
Lazy evaluation In Python,iterators and def ints(first): while True: generators allowed for lazy yield first evaluation first +=1 >>>s ints(1) Can represent large or >>next(s) infinite sequences 1 >>next(s) 2 Scheme doesn't have iterators. (define (ints first) (cons first (ints (first 1))) How about a list? Second argument to cons scm>(ints 1) is always evaluated maximum recursion depth exceeded
scm> (ints 1) maximum recursion depth exceeded Lazy evaluation ● In Python, iterators and generators allowed for lazy evaluation def ints(first): while True: yield first first += 1 (define (ints first) (cons first (ints (+ first 1))) >>> s = ints(1) >>> next(s) 1 >>> next(s) 2 ● Scheme doesn't have iterators. How about a list? Second argument to cons is always evaluated Can represent large or infinite sequences
Streams Instead of iterators, Scheme uses streams (define (ints first) (define (ints first) (cons first (cons-stream first (ints (first 1))) (ints (first 1))) scm>(ints 1) scm>(ints 1) maximum recursion depth exceeded (1.#[promise (not forced)]) Lazy evaluation,just like iterators in Python
Streams Instead of iterators, Scheme uses streams (define (ints first) (cons first (ints (+ first 1))) scm> (ints 1) maximum recursion depth exceeded (define (ints first) (cons-stream first (ints (+ first 1))) scm> (ints 1) (1 . #[promise (not forced)]) Lazy evaluation, just like iterators in Python