CS 61A Discussion 10 Streams (and Interpreters) Albert Xu Kaavya - - PowerPoint PPT Presentation

cs 61a discussion 10
SMART_READER_LITE
LIVE PREVIEW

CS 61A Discussion 10 Streams (and Interpreters) Albert Xu Kaavya - - PowerPoint PPT Presentation

CS 61A Discussion 10 Streams (and Interpreters) Albert Xu Kaavya Shah Slides: albertxu.xyz/teaching/cs61a/ *part of these slides sourced from Jemmy and Is Final Review Fall 18 Streams Streams are lazy linked lists - the first element


slide-1
SLIDE 1

CS 61A Discussion 10

Streams (and Interpreters)

Slides: albertxu.xyz/teaching/cs61a/

Albert Xu Kaavya Shah

*part of these slides sourced from Jemmy and I’s Final Review Fall ‘18

slide-2
SLIDE 2

Streams

  • Streams are lazy linked lists - the first element of each pair is

calculated, but the rest is not calculated until we explicitly ask for it.

slide-3
SLIDE 3

Streams

  • Streams are lazy linked lists - the first element of each pair is

calculated, but the rest is not calculated until we explicitly ask for it.

  • This rest value is stored in something called a promise, which

is essentially all the data of the rest!

slide-4
SLIDE 4

Streams

  • Streams are lazy linked lists - the first element of each pair is

calculated, but the rest is not calculated until we explicitly ask for it.

  • This rest value is stored in something called a promise, which

is essentially all the data of the rest!

  • A promise stored in the rest is not evaluated until we call cdr-

stream on the stream.

slide-5
SLIDE 5

Streams

  • Streams are lazy linked lists - the first element of each pair is

calculated, but the rest is not calculated until we explicitly ask for it.

>>> (define a (cons-stream 1 (cons-stream 2 nil)))

slide-6
SLIDE 6

Streams

  • Streams are lazy linked lists - the first element of each pair is

calculated, but the rest is not calculated until we explicitly ask for it.

>>> (define a (cons-stream 1 (cons-stream 2 nil)))

1 …

a

slide-7
SLIDE 7

Streams

  • Streams are lazy linked lists - the first element of each pair is

calculated, but the rest is not calculated until we explicitly ask for it.

>>> (define a (cons-stream 1 (cons-stream 2 nil)))

1 …

a

slide-8
SLIDE 8

Streams

  • Streams are lazy linked lists - the first element of each pair is

calculated, but the rest is not calculated until we explicitly ask for it.

>>> (define a (cons-stream 1 (cons-stream 2 nil))) >>> (cdr-stream a) (2 . #[promise(not forced)])

1

a

2 …

slide-9
SLIDE 9

Streams

  • Streams are lazy linked lists - the first element of each pair is

calculated, but the rest is not calculated until we explicitly ask for it.

1

>>> (define a (cons-stream 1 (cons-stream 2 nil)))

a

>>> (cdr-stream a) (2 . #[promise(not forced)])

2 …

Takeaway: notice how calling cdr-stream

  • n a stream actually mutates that stream!
slide-10
SLIDE 10

Why Streams?

  • Streams can represent infinite sequences!
slide-11
SLIDE 11

Why Streams?

  • Streams can represent infinite sequences!

>>> (define (naturals n) (cons-stream n (naturals (+ n 1))))

slide-12
SLIDE 12

Why Streams?

  • Streams can represent infinite sequences!

>>> (define (naturals n) (cons-stream n (naturals (+ n 1)))) >>> (naturals 1) (1 #[promise (nf)])

slide-13
SLIDE 13

Why Streams?

  • Streams can represent infinite sequences!

>>> (define (naturals n) (cons-stream n (naturals (+ n 1)))) >>> (naturals 1) (1 #[promise (nf)]) >>> (cdr-stream (naturals 1)) (2 #[promise (nf)])

slide-14
SLIDE 14

Why Streams?

  • Streams can represent infinite sequences!
  • So why doesn’t Python have streams?

>>> (define (naturals n) (cons-stream n (naturals (+ n 1)))) >>> (naturals 1) (1 #[promise (nf)]) >>> (cdr-stream (naturals 1)) (2 #[promise (nf)])

slide-15
SLIDE 15

Why Streams?

  • Streams can represent infinite sequences!
  • So why doesn’t Python have streams?
  • Because generators can represent infinite sequences too!

>>> (define (naturals n) (cons-stream n (naturals (+ n 1)))) >>> (naturals 1) (1 #[promise (nf)]) >>> (cdr-stream (naturals 1)) (2 #[promise (nf)])

slide-16
SLIDE 16

Questions

  • 1. How does a stream differ from a list in Scheme?
  • 2. What arguments does cons-stream take in, and how

does it differ from cons?

  • 3. What’s so special about cdr-stream - what’s the

difference between it and cdr?

slide-17
SLIDE 17

Review Questions

  • 1. How does a stream differ from a list in Scheme?

The rest of a stream is only computed when we first access it, xa in a Scheme linked list we compute all the elements when it’s

  • created. This allows us to create infinite streams!
  • 2. What arguments does cons-stream take in, and how

does it differ from cons?

Just like cons, cons-stream takes in a 1) a first element and 2) a rest which is another stream or nil, but instead of evaluating the second operand, it stores it as data in the promise!

slide-18
SLIDE 18

Review Questions

  • 3. What’s so special about cdr-stream - what’s the

difference between it and cdr?

On the first call to cdr-stream on that stream, the expression that was unevaluated and stored upon construction is now evaluated to return the rest of the stream. This value is stored so that on subsequent calls to cdr-stream, the rest no longer needs to be computed.

slide-19
SLIDE 19

Review Questions

  • 3. What’s so special about cdr-stream - what’s the

difference between it and cdr?

On the first call to cdr-stream on that stream, the expression that was unevaluated and stored upon construction is now evaluated to return the rest of the stream. This value is stored so that on subsequent calls to cdr-stream, the rest no longer needs to be computed.

Takeaway: each element in the stream is only computed once, and anytime in the future we look up that element we use that stored value

slide-20
SLIDE 20
slide-21
SLIDE 21

Thanks for coming.

Have a great rest of your week! :)

Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/