[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Axiom-developer] [Streams] (new)

From: Bill Page
Subject: [Axiom-developer] [Streams] (new)
Date: Fri, 18 Feb 2005 18:49:37 -0600

Streams display only a few of their initial elements. Otherwise, they
are “lazy”: they only compute elements when you ask for them.

Series and streams make no attempt to compute all their elements!
Rather, they stand ready to deliver elements on demand.

Section 0.5.3 Streams

  Streams are infinite lists which have the ability to calculate
the next element should it be required. For example, a stream of
positive integers and a list of prime numbers can be generated by:
[i for i in 1..]

Another method of creating streams is to use the generate(f,a)
function. This applies its first argument repeatedly onto its
second to produce the stream::

   ![a, f (a), f ( f (a)), f ( f ( f (a))) . . .]

Given that the function nextPrime returns the lowest prime number
greater than its argument we can generate a stream of primes as

primes := generate(nextPrime,2)$Stream Integer

A stream is a structure that (potentially) has an infinite number
of distinct elements. Think of a stream as an “infinite list” where
elements are computed successively.

Axiom represents streams by a collection of already-computed
elements together with a function to compute the next element
“on demand.” Asking for the n-th element causes elements 1 through
n to be evaluated.

Streams can also be finite or cyclic. They are implemented by a
linked list structure similar to lists and have many of the same
operations. For example, first and rest are used to access elements
and successive nodes of a stream.

In contract, a **set** is a collection of elements where duplication
and order is irrelevant. Sets are always finite and have no
corresponding structure like streams for infinite collections.

By default, Axiom tries to compute and display the first ten elements
of a stream.Use:
)set streams calculate 7

to change the default to something else, e.g. 7. This also affects the
number of terms computed and displayed for power series.

You can also create lists of streams, streams of lists and streams
of streams. Here is a stream of streams.
[ [i/j for i in j+1..] for j in 1..]

Section 5.6 An Example: Streams of Primes

  Create a stream of primes.
primes : Stream Integer := [i for i in 2.. | prime? i]

A more elegant way, however, is to use the generate operation as
show above.

Once the stream is generated, you might only be interested in
primes starting at a particular value.
smallPrimes := [p for p in primes | p > 1000]

Here are the first 11 primes greater than 1000.
[p for p in smallPrimes for i in 1..11]

Here is a stream of primes between 1000 and 1200.
[p for p in smallPrimes while p < 1200]

Finally, we can convert a finite stream such as above to
a simple list.
thesePrimes:=entries complete %

And now we can test for membership in the usual way.

forwarded from

reply via email to

[Prev in Thread] Current Thread [Next in Thread]