Difference between revisions of "Thunks and Streams Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 3: Line 3:
  
 
=Code To Use=
 
=Code To Use=
[https://docs.racket-lang.org/reference/define.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29 define-syntax]
+
* [https://docs.racket-lang.org/reference/define.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29 define-syntax]
 
+
* [https://docs.racket-lang.org/reference/lambda.html?q=lambda#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._lambda%29%29 lambda]
[https://docs.racket-lang.org/reference/lambda.html?q=lambda#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._lambda%29%29 lambda]
+
* [https://docs.racket-lang.org/reference/procedures.html#%28def._%28%28quote._~23~25kernel%29._procedure~3f%29%29 procedure?]
 
+
* [https://docs.racket-lang.org/reference/procedures.html?q=procedure-arity#%28def._%28%28quote._~23~25kernel%29._procedure-arity%29%29 procedure-arity]
[https://docs.racket-lang.org/reference/procedures.html#%28def._%28%28quote._~23~25kernel%29._procedure~3f%29%29 procedure?]
+
* [https://docs.racket-lang.org/reference/pairs.html?q=cons#%28def._%28%28quote._~23~25kernel%29._pair~3f%29%29 pair?]
 
+
* [https://docs.racket-lang.org/reference/pairs.html?q=cons#%28def._%28%28quote._~23~25kernel%29._cons%29%29 cons]
[https://docs.racket-lang.org/reference/procedures.html?q=procedure-arity#%28def._%28%28quote._~23~25kernel%29._procedure-arity%29%29 procedure-arity]
+
* [https://docs.racket-lang.org/reference/pairs.html?q=cons#%28def._%28%28quote._~23~25kernel%29._car%29%29 car]
 
+
* [https://docs.racket-lang.org/reference/pairs.html?q=cons#%28def._%28%28quote._~23~25kernel%29._cdr%29%29 cdr]
[https://docs.racket-lang.org/reference/pairs.html?q=cons#%28def._%28%28quote._~23~25kernel%29._pair~3f%29%29 pair?]
+
* [https://docs.racket-lang.org/reference/values.html#%28def._%28%28quote._~23~25kernel%29._values%29%29 values]
 
+
* [https://docs.racket-lang.org/reference/exns.html?q=raise#%28def._%28%28quote._~23~25kernel%29._raise%29%29 raise]
[https://docs.racket-lang.org/reference/pairs.html?q=cons#%28def._%28%28quote._~23~25kernel%29._cons%29%29 cons]
 
 
 
[https://docs.racket-lang.org/reference/pairs.html?q=cons#%28def._%28%28quote._~23~25kernel%29._car%29%29 car]
 
 
 
[https://docs.racket-lang.org/reference/pairs.html?q=cons#%28def._%28%28quote._~23~25kernel%29._cdr%29%29 cdr]
 
  
 
=Code To Implement=
 
=Code To Implement=

Revision as of 10:12, 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

Code To Implement

file: src/main/racket/hw4/hw4.rkt Racket-logo.svg
functions: thunk?
thunk-that
dethunk-that
value-next-stream-pair-from-stream
stream-cons-ensuring-stream-prime-is-thunk

Thunk Utilities

A thunk is a 0 argument function. We will write functions to check if an expression is a thunk, a MACRO to create a thunk from an expression, and a function to evaluate a thunk.

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 takes a parameter e creates thunk. that is: wraps e in a zero argument function.

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

dethunk-that

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

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

If thunking and expression wraps an expression in a single argument function, then de-thunking is simply calling that function.

NOTE: It may seem unnecessary to use dethunk-that when implementing Lab4, when you could simply (thunk)... that is "call the thunk". Still, you are encouraged to use dethunk-that as a bit of verbosity can sometimes help in debugging a sea already full of parentheses.

Stream Utilities

value-next-stream-pair-from-stream

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

(define (value-next-stream-pair-from-stream s)
   (error 'not-yet-implemented))

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 s-prime. Do NOT create a thunk.

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