1
votes

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
What does it mean for an infinite stream to have no elements? Then it's not infinite.Barmar
y that would be my intuition aswell however the assignment says exactly that "Define an infinite stream that has no elements"SyntaxIsNotTheProblem
Sounds like a good question for the instructor.Barmar

1 Answers

2
votes

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 () they write:

There is a distinguishable object, the-empty-stream, which cannot be the result of any cons-stream operation, and which can be identified with the predicate stream-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.