We just learned about streams in one of my programming courses. I worked my way already through this weeks assigned tasks except this one: Define an infinite stream that has no elements. I already scimmed through the 3.5 chapter of the SICP book but I still not really get what the trick is. Some hints would be awesome ty ^^
1 Answers
The answer depends on the exact type of streams you are using.
Basically there are two different types of streams:
- empty streams
- streams consisting of an element followed by an stream
Therefore you need two different types of values to represent the two types.
In SICP (sicp) they write:
There is a distinguishable object,
the-empty-stream
, which cannot be the result of anycons-stream
operation, and which can be identified with the predicatestream-null?
One way to define a distinguishable (unique) object in Scheme is to allocate an new cons cell - its address is unique to that cell.
(define the-empty-stream (list 'this-is-the-empty-stream))
(define (stream-null? x) (eq? x the-empty-stream))
In Racket one would simply use a struct to represent the empty stream
(struct the-empty-stream ())
(define stream-null? the-empty-stream?)
Functions such as stream->list
need to handle both types of lists.
Back to your question: If you are using SICP streams the answer is simple.
The expression the-empty-stream
will give you an empty stream.
If you are defining your own, you will need to choose a way to represent an empty stream - and then carefully modify all the stream functions you have to handle the empty stream too.