6.2.901.900
6 Streams
Streams are nothing but lazy lists. They are similar to ordinary
lists and they provide the same functionality as that of lists. The
difference between Streams and lists is that they are lazy in nature and
each cell of a Stream is suspended and is forced only when required. Streams
have been used in some of the below mentioned data structures. Since
each suspention comes with a little overhead, Streams should be used
only when there is a good enough reason to do so.
A stream of type A.
Function stream creates a Stream with the given inputs.
Example: |
> (stream 1 2 3 4 5 6) | - : (Rec | g1046702 | (U Null | (Boxof (U (Pairof Integer g1046702) (-> (Pairof Integer g1046702)))))) |
| '#&(1 . #&(2 . #&(3 . #&(4 . #&(5 . #&(6)))))) |
|
In the above example, the stream obtained will be similar to lists but will
lazy in nature. It will have 1 as its first element.
An empty stream.
Function
empty-stream? takes a Stream checks if the given
stream is empty.
Function
stream-cons takes an element and a stream and adds
the given element to the given stream.
Example: |
> (stream-cons 10 (stream 1 2 3 4 5 6)) | - : (Boxof | (-> (Pairof | Positive-Byte | (Rec | g1046849 | (U Null | (Boxof | (U (Pairof Integer g1046849) | (-> (Pairof Integer g1046849))))))))) |
| '#&#<procedure:.../pfds/stream.rkt:41:7> |
|
In the above example, (stream-cons 10 (stream 1 2 3 4 5 6))
returns the stream (stream 10 1 2 3 4 5 6).
Function
stream-car takes a stream and returns the first element
of the given stream. If the given stream is empty, then it throws an error.
Function
stream-cdr takes a stream and returns the same stream
but without the first element of the given stream.
Examples: |
> (stream-cdr (stream 1 2 3 4 5 6)) | - : (Rec | g1047084 | (U Null | (Boxof (U (Pairof Integer g1047084) (-> (Pairof Integer g1047084)))))) |
| '#&(2 . #&(3 . #&(4 . #&(5 . #&(6))))) | > (stream-cdr empty-stream) | stream-cdr: given stream is empty |
|
In the above example, (stream-cdr strm) returns
(stream 2 3 4 5 6).
Function
stream-append takes two streams and creates a new
stream by appending the second stream to the end of first stream.
Examples: |
> (define strm1 (stream 1 2 3 4 5 6)) | | > (define strm2 (stream 51 32 42)) | | > (stream-append strm1 strm2) | - : (Rec | g1047308 | (U Null | (Boxof (U (Pairof Integer g1047308) (-> (Pairof Integer g1047308)))))) |
| '#&#<procedure:.../pfds/stream.rkt:41:7> |
|
In the above example, (stream-append strm1 strm2) returns the stream,
(stream 1 2 3 4 5 6 51 32 42).
Function
stream-reverse takes a streams and gives a reversed
stream back.
Example: |
> (stream-reverse (stream 1 2 3 4 5 6)) | - : (Rec | g1047426 | (U Null | (Boxof (U (Pairof Integer g1047426) (-> (Pairof Integer g1047426)))))) |
| '#&#<procedure:.../pfds/stream.rkt:41:7> |
|
In the above example, (stream-reverse (stream 1 2 3 4 5 6)) returns
(stream 6 5 4 3 2 1).
Function
stream->list takes a stream and returns a list
of elements which are in the same order as in the stream.
Function
drop takes an integer(say n) and a stream and creates a
new stream which is same as the given stream but without the first n elements
of the input stream. If the number of elements in the given stream is less
than n, then
drop throws an error.
Examples: |
> (drop 3 (stream 1 2 3 4 5 6)) | - : (Rec | g1047669 | (U Null | (Boxof (U (Pairof Integer g1047669) (-> (Pairof Integer g1047669)))))) |
| '#&(4 . #&(5 . #&(6))) | > (drop 10 (stream 1 2 3 4 5 6)) | drop: not enough elements to drop |
|
In the above example, (drop 3 (stream 1 2 3 4 5 6)) returns
(stream 4 5 6).
Function
take takes an integer(say n) and a stream and creates a
new stream with the first n elements of the input stream. If the number of
elements in the given stream is less than n, then
take throws an
error.
(take 5 (stream 1)) does not
throw any error because of its lazy nature. take returns a suspension
rather than finishing the whole computation.
Examples: |
> (take 3 (stream 1 2 3 4 5 6)) | - : (Rec | g1047905 | (U Null | (Boxof (U (Pairof Integer g1047905) (-> (Pairof Integer g1047905)))))) |
| '#&#<procedure:.../pfds/stream.rkt:41:7> | > (take 5 (stream 1)) | - : (Rec | g1048023 | (U Null | (Boxof (U (Pairof Integer g1048023) (-> (Pairof Integer g1048023)))))) |
| '#&#<procedure:.../pfds/stream.rkt:41:7> |
|
In the above example, (take 3 (stream 1 2 3 4 5 6)) returns
(stream 1 2 3).