Difference between revisions of "Thunks and Streams Assignment"
(→thunk?) |
(→thunk?) |
||
Line 28: | Line 28: | ||
(error 'not-yet-implemented))</nowiki> | (error 'not-yet-implemented))</nowiki> | ||
− | : [https://docs.racket-lang.org/reference/booleans.html #t] | + | true: [https://docs.racket-lang.org/reference/booleans.html #t] |
− | : [https://docs.racket-lang.org/reference/booleans.html #f] | + | |
+ | false: [https://docs.racket-lang.org/reference/booleans.html #f] | ||
+ | |||
+ | is a function: [https://docs.racket-lang.org/reference/procedures.html#%28def._%28%28quote._~23~25kernel%29._procedure~3f%29%29 procedure?] | ||
+ | |||
+ | number of parameters: [https://docs.racket-lang.org/reference/procedures.html?q=procedure-arity#%28def._%28%28quote._~23~25kernel%29._procedure-arity%29%29 procedure-arity] | ||
===thunk-that=== | ===thunk-that=== |
Revision as of 01:48, 16 March 2020
Contents
Motivation
It is all too easy to make a simple mistake on the Streams_Assignment 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 | |
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
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...