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
, assumed to be a nonnegative integer.
Lazy? Yes: The result stream forces the application of f
for the next element
Parameters:
Name | Type | Description |
---|---|---|
f |
function | unary function |
n |
number | given nonnegative 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 number exceeds (>
) end
.
Lazy? Yes: The result stream forces the construction of
each next element
Parameters:
Name | Type | Description |
---|---|---|
start |
number | starting number |
end |
number | ending number |
Returns:
stream from
start
to end
- Type
- stream
eval_stream(s, n) → {list}
Constructs the list of the first
n
elements
of a given stream s
Lazy? Sort-of: eval_stream
only forces the computation of
the first n
elements, and leaves the rest of
the stream untouched.
Parameters:
Name | Type | Description |
---|---|---|
s |
stream | given stream |
n |
number | nonnegative number of elements to place in result list |
Returns:
result list
- Type
- list
integers_from(start) → {stream}
Returns infinite stream if integers starting
at given number
n
using a step size of 1.
Lazy? Yes: The result stream forces the construction of
each next element
Parameters:
Name | Type | Description |
---|---|---|
start |
number | starting number |
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 null.
Laziness: No: is_stream
needs to force the given stream.
Parameters:
Name | Type | Description |
---|---|---|
xs |
value | given candidate |
Returns:
whether
xs
is a stream
- Type
- boolean
list_to_stream(xs) → {stream}
Given list
xs
, returns a stream of same length with
the same elements as xs
in the same order.
Laziness: Yes: list_to_stream
goes down the list only when forced.
Parameters:
Name | Type | Description |
---|---|---|
xs |
list | given list |
Returns:
stream containing all elements of
xs
- Type
- stream
stream() → {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 list is generated,
and then a stream using list_to_stream
is generated from it.
Parameters:
Name | Type | Description |
---|---|---|
value1,value2,...,value_n |
value | 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 stream xs
.
In the result, null 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 |
---|---|---|
xs |
stream | given first stream |
ys |
stream | given second stream |
Returns:
result of appending
xs
and ys
- Type
- stream
stream_filter(pred, xs) → {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 |
---|---|---|
pred |
function | unary function returning boolean value |
xs |
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 |
---|---|---|
f |
function | unary |
xs |
stream | given stream |
Returns:
true
- Type
- boolean
stream_length(xs) → {number}
Returns the length of the stream
xs
.
Iterative process.
Lazy? No: The function needs to explore the whole stream
Parameters:
Name | Type | Description |
---|---|---|
xs |
stream | given stream |
Returns:
length of
xs
- Type
- number
stream_map(f, xs) → {stream}
Returns a stream that results from stream
xs
by 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 |
---|---|---|
f |
function | unary |
xs |
stream | given stream |
Returns:
result of mapping
- Type
- stream
stream_member(v, xs) → {stream}
Returns first postfix substream
whose head is identical to
v
(using ===
); returns null
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 |
---|---|---|
v |
value | given value |
xs |
stream | given stream |
Returns:
postfix substream that starts with
v
- Type
- stream
stream_ref(xs, 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 |
---|---|---|
xs |
stream | given stream |
n |
number | 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 |
---|---|---|
v |
value | given value |
xs |
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 |
---|---|---|
v |
value | given value |
xs |
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 |
---|---|---|
xs |
stream | given stream |
Returns:
xs
in reverse
- Type
- stream
stream_tail(xs) → {Stream}
assumes that the tail (second component) of the
pair {x} is a nullary function, and returns the result of
applying that function. Throws an exception 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 |
---|---|---|
xs |
Stream | given stream |
Returns:
result stream (if stream discipline is used)
- Type
- Stream
stream_to_list(xs) → {list}
Given stream
xs
, returns a list of same length with
the same elements as xs
in the same order.
Laziness: No: stream_to_list
needs to force the whole
stream.
Parameters:
Name | Type | Description |
---|---|---|
xs |
stream | stream |
Returns:
containing all elements of
xs
- Type
- list