Streams scm>(define s (cons-stream 1 (cons-stream 2 nil))) s scm>s Stream:(linked)list whose rest (1.#[promise (not forced)]) is lazily evaluated scm>(car s) 1 O A promise to compute I don't need the rest of this list right now.Can you Sure,I promise to compute it compute it for me later? right after I finish watching Stranger Things. scm>
scm> (define s (cons-stream 1 (cons-stream 2 nil))) s scm> s (1 . #[promise (not forced)]) scm> (car s) 1 Streams ● Stream: (linked) list whose rest is lazily evaluated ○ A promise to compute I don't need the rest of this list right now. Can you compute it for me later? scm> Sure, I promise to compute it right after I finish watching Stranger Things
Streams scm>(define s (cons-stream 1 (cons-stream 2 nil))) s scm>s cdr returns the rest of a list (1.#[promise (not forced)]) O For normal lists,the rest is scm>(cdr s) #[promise (not forced)] another list O The rest of a stream is a promise to compute the list I want the cdr of the list now. Just one more episode.I'm almost done with season 3. scm>
scm> (define s (cons-stream 1 (cons-stream 2 nil))) s scm> s (1 . #[promise (not forced)]) scm> (cdr s) #[promise (not forced)] Streams ● cdr returns the rest of a list ○ For normal lists, the rest is another list ○ The rest of a stream is a promise to compute the list I want the cdr of the list now. scm> Just one more episode. I'm almost done with season 3
Streams scm>(define s (cons-stream 1 (cons-stream 2 nil))) s scm>s (1.#[promise (not forced)]) cdr-stream forces Scheme to scm>(cdr-stream s) compute the rest (2.#[promise (not forced)]) scm>(cdr-stream (cdr-stream s)) () cdr-stream! Fine!Here's your stupid list. scm>
scm> (define s (cons-stream 1 (cons-stream 2 nil))) s scm> s (1 . #[promise (not forced)]) scm> (cdr-stream s) (2 . #[promise (not forced)]) scm> (cdr-stream (cdr-stream s)) () Streams ● cdr-stream forces Scheme to compute the rest cdr-stream ! scm> Fine! Here's your stupid list
Streams Remember,a stream is just a regular Scheme pair whose second element is a promise scm>(define s (cons-stream 1 (cons-stream 2 nil))) scm>(cdr s) #[promise (not forced)] scm>(cdr-stream s) (2 #[promise (not forced)]) scm>(cdr-stream (cdr-stream s)) IF A PROMISE CONTAINS () AN EMPTY LIST Promise: Promise: (cons-stream 2 nil) nil 1 2 IS IT AN EMPTY PROMISE?
scm> (cdr-stream s) (2 . #[promise (not forced)]) scm> (define s (cons-stream 1 (cons-stream 2 nil))) s scm> (cdr s) #[promise (not forced)] Streams Remember, a stream is just a regular Scheme pair whose second element is a promise 1 Promise: (cons-stream 2 nil) scm> (cdr-stream (cdr-stream s)) () 2 Promise: nil