Difference between revisions of "Thunks and Streams Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 49: Line 49:
  
 
  <code>(raise (error "not a thunk" stream-prime))</code>
 
  <code>(raise (error "not a thunk" stream-prime))</code>
 +
 +
If <code>stream-prime</code> is a thunk it should simply cons <code>value</code> and <code>stream-prime</code>.  Do '''NOT''' create a thunk.
  
 
WARNING: what could happen if you called <code>plausible-stream?</code> on <code>stream-prime</code>?
 
WARNING: what could happen if you called <code>plausible-stream?</code> on <code>stream-prime</code>?

Revision as of 03:43, 9 July 2019

Code To Use

define-syntax

lambda

procedure?

procedure-arity

pair?

cons

car

cdr

Code To Implement

Thunk Utilities

thunk?

define a function thunk? which returns #t if the specified parameter is a thunk, #f otherwise.

thunk MACRO

define a macro thunk which creates thunk

dethunk

define a function dethunk which takes a thunk parameter th and returns the result of invoking th.

Stream Utilities

plausible-stream?

define a function plausible-stream? which returns #t if the specified parameter is a thunk which when invoked returns a pair whose cdr is a thunk, #f otherwise.

WARNING: what could happen if you called plausible-stream? recursively on the cdr?

next-value-from-stream

define a function next-value-from-stream which takes a stream parameter and returns the next value of that stream.

next-stream-from-stream

define a function next-stream-from-stream which takes a stream parameter and returns the next stream of that stream.

stream-next

BONUS method not officially part of the studio but a good idea.

define a function stream-next which takes a stream parameter and returns a pair of values for the next value and the next stream.

stream-cons

define a function stream-cons which takes two parameters value and stream-prime. if the specified stream-prime parameter is not a thunk then an error should be raised:

(raise (error "not a thunk" stream-prime))

If stream-prime is a thunk it should simply cons value and stream-prime. Do NOT create a thunk.

WARNING: what could happen if you called plausible-stream? on stream-prime?

Stream App

flip-flop-stream

define flip-flop-stream which produces #t #f #t #f #t #f #t #f...