Programming Languages Streams MIDTERM Wednesday? Review A thunk - - PowerPoint PPT Presentation

programming languages streams midterm wednesday review
SMART_READER_LITE
LIVE PREVIEW

Programming Languages Streams MIDTERM Wednesday? Review A thunk - - PowerPoint PPT Presentation

Programming Languages Streams MIDTERM Wednesday? Review A thunk is a function of no arguments used to explicitly delay a result. (delay expression) => returns a thunked version of expression has to be implemented as a


slide-1
SLIDE 1

Programming Languages Streams

slide-2
SLIDE 2
slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6

MIDTERM – Wednesday?

slide-7
SLIDE 7

Review

  • A thunk is a function of no arguments used to explicitly delay a

result.

  • (delay expression) => returns a thunked version of

expression – has to be implemented as a special form so that expression won't be evaluated until we force it. – Once forced, later forces won't re-evaluate expression, but rather the same value will be returned for every subsequent force. – Called a promise. (in that we say delay returns a promise)

  • (force promise) => returns the value of the original

delayed expression, either by evaluating it, or saving the cached value.

slide-8
SLIDE 8

Example

(define x 1) (define y (delay x) (force y) (set! x 2) (force y)

slide-9
SLIDE 9

Streams

  • One common use for promises is to create a new data type

called a stream.

  • Stream == List

– Only difference is the car of a stream is eager (evaluated normally), but the cdr is lazy (implemented as a promise). – (Car and cdr of normal lists are eager.)

  • Create a stream with stream-cons:

(define-syntax-rule (stream-cons first rest) (cons first (delay rest)))

  • This code creates a special form that literally replaces every call

to stream-cons with the line (cons <first arg> (delay <2nd arg>)).

  • A normal function wouldn't work because it would evaluate both

arguments, but we want to delay evaluation of the rest argument.

slide-10
SLIDE 10

Useful stream functions

List version Stream version '() the-empty-stream null? stream-null? car stream-car cdr stream-cdr stream->list list-ref stream-ref stream-enumerate

Most of these are just the list functions we know and love with the prefix "stream-"

slide-11
SLIDE 11

Finite Streams

  • Not any more useful than lists.

– (stream-cons 1 
 (stream-cons 2 
 (stream-cons 3 the-empty-stream)))

  • The power of streams comes from making infinite streams

– Impossible to do with lists. – Easy with streams because we don't explicitly represent all the values (since there are an infinite number of them). – Instead, we represent the first one explicitly, and then promise to provide the next one as soon as it's needed.

slide-12
SLIDE 12

Two common stream idioms

  • Consider these two variations:
  • (define (make-constant-stream item)

(stream-cons item 
 (make-constant-stream item))) (define ones-alt (make-constant-stream 1))


  • (define ones (stream-cons 1 ones))
slide-13
SLIDE 13
  • Create an infinite stream of integers, starting at zero and

increasing by one. – Hint: define a function that takes an argument x and returns a stream of integers starting from x.

  • Define a function stream-map that duplicates the functionality
  • f map for streams.
  • Define a function stream-map2 that works like map2 on proj2

(takes a function of two args and two streams).

  • Define an alternate version of the infinite stream of integers

starting from zero by using stream-map and an infinite stream

  • f ones.
  • Define a function stream-filter that duplicates filter.
  • Define a function not-divisible-by that takes a stream of

integers and an integer n and removes all the integers that are divisible by n from the stream.

  • Define an infinite stream of prime numbers.

– Hint: use not-divisible-by on a stream of the ints from 2.

  • Define an infinite stream of the Fibonacci numbers.