next up previous contents index
Next: String Up: Stream Previous: orbit   Contents   Index


stream


Usage

stream t
stream f
stream h
stream(n, g)
stream(n, g, m, t)
stream(gen, t)
stream(gen, m, t)


Signatures

stream: T $\to$ %
stream: (() $\to$ T) $\to$ %
stream: ((MachineInteger, %) $\to$ T) $\to$ %
stream: (MachineInteger, MachineInteger $\to$ T) $\to$ %
stream: (MachineInteger, MachineInteger $\to$ T, MachineInteger, T) $\to$ %
stream: (Generator T, T) $\to$ %
stream: (Generator T, MachineInteger, T) $\to$ %


Parameter Type Description
n, m MachineInteger machine integers
t T an element
f () $\to$ T a function
g MachineInteger $\to$ T a function
h (MachineInteger, %) $\to$ T a function
gen Generator T a generator


Description

stream(t) returns the constant stream $[t,t,t,\dots]$, while stream(f) returns the stream obtained by successive calls to $f()$, stream(n, g) returns the stream $[g(n),g(n+1),g(n+2),\dots]$, stream(n, g, m, t) returns the eventually constant stream $[g(n),g(n+1),g(n+2),\dots,g(m-1),t,t,t,\dots]$ and stream(h) returns the stream $[s_0,s_1,s_2,\dots]$ where $s_k = h(k, s_0, \dots s_{i-k})$ for any $k \ge 0$. When using generators, stream(gen, t) returns the stream $[x \mbox{ for } x \mbox{ in } gen]$ followed by $[t,t,t,\dots]$ if $gen$ is finite, while stream(gen, m, t) returns the first $m$ elements of the stream $[x \mbox{ for } x \mbox{ in } gen]$ followed by $[t,t,t,\dots]$.


See Also

orbit


Remarks

The function $g$ must be defined for all $n\le k$ (resp. $n\le k < m$) and $h$ must be defined for all $n \ge 0$, even if you intend to use set! to set specific values of the stream later. Note that stream(f) does not necessarily returns a constant stream since each call to $f()$ can side-effect its environment. Constant or eventually constant streams do not repeatedly store their constant value and are therefore preferable. For example, ${\tt stream(0)}$ stores only one value no matter how many values are requested, while ${\tt stream(():MachineInteger +-> 0)}$ stores $n$ times $0$ when its ${n}^{{\rm th}}$ element is requested.


Example

The following code creates the stream of the Fibonnaci numbers and prints the first 10 of them:
	import from MachineInteger, AldorInteger, Stream AldorInteger;
	fib(n:MachineInteger, f:Stream AldorInteger):AldorInteger == {
		n = 0 or n = 1 => 1;
		f(n-1) + f(n-2);
	}
	sfib := stream fib;
	a := sfib.9;	-- computes sfib.0 to sfib.9
	stdout << a << newline;


next up previous contents index
Next: String Up: Stream Previous: orbit   Contents   Index
Manuel Bronstein 2004-06-28