Difference between revisions of "Thunks and Streams Assignment"
(8 intermediate revisions by the same user not shown) | |||
Line 17: | Line 17: | ||
=Code To Implement= | =Code To Implement= | ||
− | {{RacketToImplement| | + | {{RacketToImplement|uw4|thunk?<br>thunk-that<br>dethunk-that<br>value-next-stream-pair-from-stream<br>stream-cons-ensuring-stream-prime-is-thunk|uw4}} |
==Thunk Utilities== | ==Thunk Utilities== | ||
A [https://en.wikipedia.org/wiki/Thunk 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. | A [https://en.wikipedia.org/wiki/Thunk 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. | ||
Line 49: | Line 49: | ||
==Stream Utilities== | ==Stream Utilities== | ||
===destream=== | ===destream=== | ||
− | define a function <code>destream</code> which takes a <code>stream</code> parameter and | + | define a function <code>destream</code> which takes a <code>stream</code> parameter and evaluates to the resulting dethunked pair. |
(define (destream stream) | (define (destream stream) | ||
Line 56: | Line 56: | ||
A critical value of this utility function is to provide early error detection. As such, exceptions should be raised if either | A critical value of this utility function is to provide early error detection. As such, exceptions should be raised if either | ||
− | * the parameter <code> | + | * the parameter <code>stream</code> is not a thunk (this can be handled by <code>dethunk-that</code>) |
− | * the result of de-thunking the stream is not a pair | + | * the result of de-thunking the <code>stream</code> is not a pair |
* next-stream of the pair is not thunk | * next-stream of the pair is not thunk | ||
<!-- | <!-- | ||
Line 77: | Line 77: | ||
<nowiki>425/231 => 1 w/ remainder 194</nowiki> | <nowiki>425/231 => 1 w/ remainder 194</nowiki> | ||
--> | --> | ||
+ | |||
===stream-cons-ensuring-stream-prime-is-thunk=== | ===stream-cons-ensuring-stream-prime-is-thunk=== | ||
Define a function <code>cons-with-thunk-check-on-next-stream</code> which takes two parameters <code>value</code> and <code>next-stream</code>. | Define a function <code>cons-with-thunk-check-on-next-stream</code> which takes two parameters <code>value</code> and <code>next-stream</code>. | ||
Line 90: | Line 91: | ||
=Test= | =Test= | ||
− | {{RacketUnitTest| | + | ==thunk== |
+ | {{RacketUnitTest|thunk_test|uw4}} | ||
+ | |||
+ | ==stream== | ||
+ | {{RacketUnitTest|stream_test|uw4}} |
Revision as of 21:06, 27 October 2022
Contents
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, alerting us if we make a mistake.
Code To Use
- #t true
- #f false
- define-syntax
- lambda
- procedure?
- procedure-arity number of arguments a function takes Arity on Wikipedia
- pair?
- cons
- car
- cdr
- values
- raise
Code To Implement
file: | src/main/racket/uw4/uw4.rkt | |
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))
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))
Thunks are useful for delaying the evaluation of expressions. As such thunk-that
must be declared as a macro and not a function. Unlike Haskell which has lazy evaluation, Racket (and most other languages) eagerly evaluates function arguments.
dethunk-that
define a function dethunk
which takes a thunk parameter e
and returns the result of invoking e
.
(define (dethunk-that thunk) (error 'not-yet-implemented))
If thunking and expression wraps an expression in a single argument function, then de-thunking is simply calling that function.
If the parameter thunk
is not a thunk?
then dethunk-that
should raise an argument error.
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
destream
define a function destream
which takes a stream
parameter and evaluates to the resulting dethunked pair.
(define (destream stream) (error 'not-yet-implemented))
A critical value of this utility function is to provide early error detection. As such, exceptions should be raised if either
- the parameter
stream
is not a thunk (this can be handled bydethunk-that
) - the result of de-thunking the
stream
is not a pair - next-stream of the pair is not thunk
stream-cons-ensuring-stream-prime-is-thunk
Define a function cons-with-thunk-check-on-next-stream
which takes two parameters value
and next-stream
.
((define (cons-with-thunk-check-on-next-stream element next-stream) (error 'not-yet-implemented))
If the specified next-stream
parameter is not a thunk then an error should be raised:
(raise-argument-error "next-stream" "thunk?" next-stream)
Do NOT create a thunk. If next-stream
is a thunk it should simply cons value
and next-stream
.
Test
thunk
file: | thunk_test.rkt | Test |
source folder: | src/test/racket/uw4 |
note: ensure that you have removed all printing to receive credit for any assignment.
stream
file: | stream_test.rkt | Test |
source folder: | src/test/racket/uw4 |
note: ensure that you have removed all printing to receive credit for any assignment.