Difference between revisions of "Thunks and Streams Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 49: Line 49:
  
 
==Stream Utilities==
 
==Stream Utilities==
 +
===value-next-stream-pair-from-stream===
 +
define a function <code>stream-next</code> which takes a stream parameter and returns a pair of values for the next value and the next stream.
  
===plausible-stream?===
+
===next-value-from-stream===
define a function <code>plausible-stream?</code> which returns #t if the specified parameter is a thunk which when invoked returns a pair whose cdr is a thunk, #f otherwise.
+
provided function <code>next-value-from-stream</code> which takes a stream parameter and returns the next value of that stream.
 
 
WARNING: what could happen if you called <code>plausible-stream?</code> recursively on the <code>cdr</code>?
 
  
===next-value-from-stream===
+
(define (value-from-stream s)
define a function <code>next-value-from-stream</code> which takes a stream parameter and returns the next value of that stream.
+
  (let-values ([(value s-prime) (value-next-stream-pair-from-stream s)])
 +
    value))
  
 
===next-stream-from-stream===
 
===next-stream-from-stream===
define a function <code>next-stream-from-stream</code> which takes a stream parameter and returns the next stream of that stream.
+
provided function <code>next-stream-from-stream</code> which takes a stream parameter and returns the next stream of that stream.
  
===stream-next===
+
(define (next-stream-from-stream s)
BONUS method not officially part of the studio but a good idea.
+
  (let-values ([(value s-prime) (value-next-stream-pair-from-stream s)])
 
+
    s-prime))
define a function <code>stream-next</code> which takes a stream parameter and returns a pair of values for the next value and the next stream.
 
  
===stream-cons===
+
===stream-cons-ensuring-stream-prime-is-thunk===
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:
+
define a function <code>stream-cons-ensuring-stream-prime-is-thunk</code> which takes two parameters <code>value</code> and <code>s-prime</code>.  if the specified <code>s-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 thunk" stream-prime))</code>
  
If <code>stream-prime</code> is a thunk it should simply [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28quote._~23~25kernel%29._cons%29%29 cons] <code>value</code> and <code>stream-prime</code>.  Do '''NOT''' create a thunk.
+
If <code>s-prime</code> is a thunk it should simply [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28quote._~23~25kernel%29._cons%29%29 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>?
+
(define (stream-cons-ensuring-stream-prime-is-thunk value s-prime)
 +
  (error 'not-yet-implemented))
  
 
==Stream App==
 
==Stream App==
 
===flip-flop-stream===
 
===flip-flop-stream===
 
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 09:44, 16 March 2020

Motivation

It is all too easy to make a simple mistake on the Streams Lab and have it burn way to much of ones time. We will build some utility functions that will hopefully make our code more clear, as well as raise better errors sooner when we make a mistake.

Code To Use

define-syntax

lambda

procedure?

procedure-arity

pair?

cons

car

cdr

Code To Implement

file: src/main/racket/hw4/hw4.rkt Racket-logo.svg
functions: thunk?
thunk-that
dethunk-that
...

Thunk Utilities

thunk?

define a function thunk? which returns whether the specified parameter is a thunk or not.

(define (thunk? th)
    (error 'not-yet-implemented))

true: #t

false: #f

is a function: procedure?

number of parameters: procedure-arity

thunk-that

define a MACRO thunk-that which creates thunk

(define-syntax-rule (thunk-that e)
   (error 'not-yet-implemented))

dethunk-that

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

(define (dethunk-that e)
   (error 'not-yet-implemented))

Stream Utilities

value-next-stream-pair-from-stream

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

next-value-from-stream

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

(define (value-from-stream s)
 (let-values ([(value s-prime) (value-next-stream-pair-from-stream s)])
   value))

next-stream-from-stream

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

(define (next-stream-from-stream s)

 (let-values ([(value s-prime) (value-next-stream-pair-from-stream s)])
   s-prime))

stream-cons-ensuring-stream-prime-is-thunk

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

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

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

(define (stream-cons-ensuring-stream-prime-is-thunk value s-prime)
  (error 'not-yet-implemented))

Stream App

flip-flop-stream

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