Functions
build_stream(f, n) → {stream}
Makes a stream with n
elements by applying the unary function f
to the numbers 0 to n-1.
Lazy? Yes: The result stream forces the application of f
for the next element
Parameters:
| Name | Type | Description |
|---|---|---|
|
function | given unary function |
|
int | given integer |
Returns:
resulting stream
- Type
- stream
enum_stream(start, end) → {stream}
Returns a stream that enumerates
numbers starting from start using a step size of 1, until
the integer exceeds (
>) end.
Lazy? Yes: The result stream forces the construction of
each next element
Parameters:
| Name | Type | Description |
|---|---|---|
|
integer | start - starting integer |
|
integer | end - ending integer |
Returns:
stream from start to end
- Type
- stream
eval_stream(s, n) → {linked_list}
Constructs the linked_list of the first nelements
of a given stream s
Lazy? Sort-of: eval_stream only forces the computation of
the first nelements, and leaves the rest of
the stream untouched.
Parameters:
| Name | Type | Description |
|---|---|---|
|
stream | given stream |
|
integer | given number of elements to place in result linked_list |
Returns:
result linked_list
- Type
- linked_list
integers_from(start) → {stream}
Returns infinite stream if numbers starting
at given integer n using a step size of 1.
Lazy? Yes: The result stream forces the construction of
each next element
Parameters:
| Name | Type | Description |
|---|---|---|
|
integer | start - starting integer |
Returns:
infinite stream from n
- Type
- stream
is_stream(xs) → {boolean}
Returns True if xs is a stream as defined in the textbook,
and False otherwise. Iterative process.
Recurses down the stream and checks that it ends with the empty stream None.
Laziness: No: is_stream needs to force the given stream.
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given value |
Returns:
whether xs is a stream
- Type
- boolean
llist_to_stream(xs) → {stream}
Given linked_list xs, returns a stream of same length with
the same elements as xs in the same order.
Laziness: Yes: llist_to_stream
goes down the linked_list only when forced.
Parameters:
| Name | Type | Description |
|---|---|---|
|
linked_list | given linked_list |
Returns:
stream containing all elements of xs
- Type
- stream
stream(…value1,) → {stream}
Given n values, returns a stream of length n.
The elements of the stream are the given values in the given order.
Lazy? No: A complete linked linked_list is generated,
and then a stream using llist_to_stream is generated from it.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
|
value |
<repeatable> |
value2, ...values - given values |
Returns:
stream containing all values
- Type
- stream
stream_append(xs, ys) → {stream}
Returns a stream that results from
appending the stream ys to the streamxs.
In the result, None at the end of the first argument stream
is replaced by the second argument, regardless what the second
argument consists of.
Lazy? Yes: the result stream forces the actual append operation
Parameters:
| Name | Type | Description |
|---|---|---|
|
stream | given first stream |
|
stream | given second stream |
Returns:
result of appending xs and ys
- Type
- stream
stream_filter(pred, s) → {stream}
Returns a stream that contains
only those elements of given stream xs
for which the one-argument function
pred
returns True.
Lazy? Yes: The result stream forces the construction of
each next element. Of course, the construction
of the next element needs to go down the stream
until an element is found for which pred holds.
Parameters:
| Name | Type | Description |
|---|---|---|
|
function | given pred - unary function returning boolean value |
|
stream | given stream |
Returns:
stream with those elements of xs for which pred holds.
- Type
- stream
stream_for_each(f, xs) → {boolean}
Applies unary function f to every
element of the stream xs.
Iterative process.
f is applied element-by-element:
stream_for_each(f, stream(1, 2)) results in the calls
f(1) and f(2).
Lazy? No: stream_for_each
forces the exploration of the entire stream
Parameters:
| Name | Type | Description |
|---|---|---|
|
function | given unary function |
|
stream | given stream |
Returns:
True
- Type
- boolean
stream_length(xs) → {integer}
Returns the length of the stream xs.
Iterative process.
Lazy? No: The function needs to explore the whole stream
Parameters:
| Name | Type | Description |
|---|---|---|
|
stream | given stream |
Returns:
length of xs
- Type
- integer
stream_map(f, xs) → {stream}
Returns a stream that results from stream
xsby element-wise application
of unary function f.
f is applied element-by-element:
stream_map(f, stream(1,2)) results in
the same as stream(f(1),f(2)).
Lazy? Yes: The argument stream is only explored as forced by
the result stream.
Parameters:
| Name | Type | Description |
|---|---|---|
|
function | given unary function |
|
stream | given stream |
Returns:
result of mapping
- Type
- stream
stream_member(x, s) → {stream}
Returns first postfix substream
whose head is identical to
v (using ==); returns None if the
element does not occur in the stream.
Iterative process.
Lazy? Sort-of:
stream_member
forces the stream only until the element
is found.
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given value |
|
stream | given stream |
Returns:
postfix substream that starts with x
- Type
- stream
stream_ref(s, n) → {value}
Returns the element
of stream xs at position n,
where the first element has index 0.
Iterative process.
Lazy? Sort-of: stream_ref only forces the computation of
the first n elements, and leaves the rest of
the stream untouched.
Parameters:
| Name | Type | Description |
|---|---|---|
|
stream | given stream |
|
integer | given position |
Returns:
item in xs at position n
- Type
- value
stream_remove(v, xs) → {stream}
Returns a stream that results from
xs by removing the first item from xs that
is identical (
===) to v.
Returns the original
stream if there is no occurrence.
Lazy? Yes: the result stream forces the construction of each next element
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given value |
|
stream | given stream |
Returns:
xs with first occurrence of v removed
- Type
- stream
stream_remove_all(v, xs) → {stream}
Returns a stream that results from
xs by removing all items from xs that
are identical (
==) to v.
Returns the original
stream if there is no occurrence.
Recursive process.
Lazy? Yes: the result stream forces the construction of each next
element
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given value |
|
stream | given stream |
Returns:
xs with all occurrences of v removed
- Type
- stream
stream_reverse(xs) → {stream}
Returns stream xs in reverse
order. Iterative process.
The process is iterative, but consumes space Omega(n)
because of the result stream.
Lazy? No: stream_reverse
forces the exploration of the entire stream
Parameters:
| Name | Type | Description |
|---|---|---|
|
stream | given stream |
Returns:
xs in reverse
- Type
- stream
stream_tail(xs) → {stream}
assumes that the tail (second component) of the
pair {x} expects 2 arguments, and returns the result of
applying that function. Throws an error if the argument
is not a pair, or if the tail is not a function.
Laziness: Yes: {stream_tail} only forces the direct tail
stream, but not the rest of the stream, i.e. not the tail
of the tail, etc.
Parameters:
| Name | Type | Description |
|---|---|---|
|
stream | given value |
Returns:
result stream (if stream discipline is used)
- Type
- stream
stream_to_llist(xs) → {linked_list}
Given stream xs, returns a linked_list of same length with
the same elements as xs in the same order.
Laziness: No: stream_to_llist needs to force the whole
stream.
Parameters:
| Name | Type | Description |
|---|---|---|
|
stream | given stream |
Returns:
containing all elements of xs
- Type
- linked_list