Difference between revisions of "Thunks and Streams Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 9: Line 9:
 
=Stream Utilities=
 
=Stream Utilities=
  
define a function <code>stream?</code> which returns #t if the specified parameter is a stream, #f otherwise.
+
define a function <code>plausible-stream?</code> which returns #t if the specified parameter is a thunk which evaluates to a pair whose cdr is a thunk, #f otherwise.
 +
 
 +
WARNING: what could happen if you called <code>plausible-stream?</code> recursively on the <code>cdr</code>?
  
 
define a function <code>next-value-from-stream</code> which takes a stream parameter and returns the next value of that stream.
 
define a function <code>next-value-from-stream</code> which takes a stream parameter and returns the next value of that stream.
Line 15: Line 17:
 
define a function <code>next-stream-from-stream</code> which takes a stream parameter and returns the next stream of that stream.
 
define a function <code>next-stream-from-stream</code> which takes a stream parameter and returns the next stream of that stream.
  
define a function <code>stream-cons</code> which takes two parameters <code>value</code> and <code>stream-prime</code>.  if the specified <code>stream-prime</code> parameter is not a stream then an error should be raised:
+
define a function <code>stream-cons</code> which takes two parameters <code>value</code> and <code>stream-prime</code>.  if the specified <code>stream-prime</code> parameter is not a thunk then an error should be raised:
 +
 
 +
<code>(raise (error "not a thunk" stream-prime))</code>
  
<code>(raise (error "not a stream" stream-prime))</code>
+
WARNING: what could happen if you called <code>plausible-stream?</code> on <code>stream-prime</code>?
  
 
=Stream App=
 
=Stream App=
  
 
define <code>flip-flop-stream</code> which produces #t #f #t #f #t #f #t #f...
 
define <code>flip-flop-stream</code> which produces #t #f #t #f #t #f #t #f...

Revision as of 15:51, 2 July 2019

Thunk Utilities

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

define a macro thunk which creates thunk

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

Stream Utilities

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

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

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

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

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))

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

Stream App

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