On this page:
8.18
top
up

6StreamsπŸ”— i

(require pfds/stream ) package: pfds

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 suspension comes with a little overhead, Streams should be used only when there is a good enough reason to do so.

syntax

( Stream A)

A stream of type A.

procedure

( stream a...)(Stream A)

a:A
Function stream creates a Stream with the given inputs.

Example:
> (stream 123456)

- : (Rec

g333057

(U (Boxof (U (-> (Pairof Integer g333057)) (Pairof Integer g333057)))

Null))

'#&(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.

value

empty-stream :(Stream Nothing)

An empty stream.

procedure

( empty-stream? strm)Boolean

strm:(Stream A)
Function empty-stream? takes a Stream checks if the given stream is empty.

Examples:
> (empty-stream? (stream 123456))

- : Boolean

#f

- : Boolean

#t

procedure

( stream-cons astrm)(Stream A)

a:A
strm:(Stream A)
Function stream-cons takes an element and a stream and adds the given element to the given stream.

Example:
> (stream-cons 10(stream 123456))

- : (Boxof

(-> (Pairof

Positive-Byte

(Rec

g333081

(U (Boxof

(U (-> (Pairof Integer g333081)) (Pairof Integer g333081)))

Null)))))

'#&#<procedure:...fds/pfds/stream.rkt:40:7>

In the above example, (stream-cons 10(stream 123456)) returns the stream (stream 10123456).

procedure

( stream-car strm)A

strm:(Stream A)
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.

Examples:
> (stream-car (stream 123456))

- : Integer

1

stream-car: given stream is empty

procedure

( stream-cdr strm)(Stream A)

strm:(Stream A)
Function stream-cdr takes a stream and returns the same stream but without the first element of the given stream.

Examples:
> (stream-cdr (stream 123456))

- : (Rec

g333103

(U (Boxof (U (-> (Pairof Integer g333103)) (Pairof Integer g333103)))

Null))

'#&(2 . #&(3 . #&(4 . #&(5 . #&(6)))))

stream-cdr: given stream is empty

In the above example, (stream-cdr strm) returns (stream 23456).

procedure

( stream-append strm1strm2)(Stream A)

strm1:(Stream A)
strm2:(Stream A)
Function stream-append takes two streams and creates a new stream by appending the second stream to the end of first stream.

Examples:
> (definestrm1(stream 123456))
> (definestrm2(stream 513242))
> (stream-append strm1strm2)

- : (Rec

g333125

(U (Boxof (U (-> (Pairof Integer g333125)) (Pairof Integer g333125)))

Null))

'#&#<procedure:...fds/pfds/stream.rkt:40:7>

In the above example, (stream-append strm1strm2) returns the stream, (stream 123456513242).

procedure

( stream-reverse strm)(Stream A)

strm:(Stream A)
Function stream-reverse takes a streams and gives a reversed stream back.

Example:
> (stream-reverse (stream 123456))

- : (Rec

g333134

(U (Boxof (U (-> (Pairof Integer g333134)) (Pairof Integer g333134)))

Null))

'#&#<procedure:...fds/pfds/stream.rkt:40:7>

In the above example, (stream-reverse (stream 123456)) returns (stream 654321).

procedure

( stream->list strm)(ListofA)

strm:(Stream A)
Function stream->list takes a stream and returns a list of elements which are in the same order as in the stream.

Examples:
> (stream->list (stream 123456))

- : (Listof Integer)

'(1 2 3 4 5 6)

- : (Listof Nothing)

'()

procedure

( drop numstrm)(Stream A)

num:Integer
strm:(Stream A)
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 123456))

- : (Rec

g333156

(U (Boxof (U (-> (Pairof Integer g333156)) (Pairof Integer g333156)))

Null))

'#&(4 . #&(5 . #&(6)))

> (drop 10(stream 123456))

drop: not enough elements to drop

In the above example, (drop 3(stream 123456)) returns (stream 456).

procedure

( take numstrm)(Stream A)

num:Integer
strm:(Stream A)
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 123456))

- : (Rec

g333174

(U (Boxof (U (-> (Pairof Integer g333174)) (Pairof Integer g333174)))

Null))

'#&#<procedure:...fds/pfds/stream.rkt:40:7>

> (take 5(stream 1))

- : (Rec

g333183

(U (Boxof (U (-> (Pairof Integer g333183)) (Pairof Integer g333183)))

Null))

'#&#<procedure:...fds/pfds/stream.rkt:40:7>

In the above example, (take 3(stream 123456)) returns (stream 123).

top
up

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /