Section5.5

]>
Book Contents Previous Section 5.4 Loops Next Section 5.6 An Example: Streams of Primes Book Index

## 5.5 Creating Lists and Streams with Iterators

All of what we did for loops in ugLangLoops iteration can be transformed into expressions that create lists list:created by iterator and streams. stream:created by iterator The repeat, break or iterate words are not used but all the other ideas carry over. Before we give you the general rule, here are some examples which give you the idea.

This creates a simple list of the integers from 1 to 10.

list := [i for i in 1..10]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Type: List PositiveInteger

Create a stream of the integers greater than or equal to 1.

stream := [i for i in 1..]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10,…]

Type: Stream PositiveInteger

This is a list of the prime integers between 1 and 10, inclusive.

[i for i in 1..10 | prime? i]

[2, 3, 5, 7]

Type: List PositiveInteger

This is a stream of the prime integers greater than or equal to 1.

[i for i in 1.. | prime? i]

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29,…]

Type: Stream PositiveInteger

This is a list of the integers between 1 and 10, inclusive, whose squares are less than 700.

[i for i in 1..10 while i*i < 700]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Type: List PositiveInteger

This is a stream of the integers greater than or equal to 1 whose squares are less than 700.

[i for i in 1.. while i*i < 700]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10,…]

Type: Stream PositiveInteger

Here is the general rule. collection

The general syntax of a collection is

[ collectExpression iterator 1 iterator 2 ... iterator N ]

where each iterator i is either a for or a while clause. The loop terminates immediately when the end test of any iterator i succeeds or when a return expression is evaluated in collectExpression. The value returned by the collection is either a list or a stream of elements, one for each iteration of the collectExpression.

Be careful when you use while stream:using while @{using while} to create a stream. By default, Axiom tries to compute and display the first ten elements of a stream. If the while condition is not satisfied quickly, Axiom can spend a long (possibly infinite) time trying to compute stream:number of elements computed the elements. Use )set streams calculate to change the default to something else. set streams calculate This also affects the number of terms computed and displayed for power series. For the purposes of this book, we have used this system command to display fewer than ten terms.

Use nested iterators to create lists of iteration:nested lists which can then be given as an argument to matrix.

matrix [ [x**i+j for i in 1..3] for j in 10..12]

[x+10 x 2+10 x 3+10 x+11 x 2+11 x 3+11 x+12 x 2+12 x 3+12]

Type: Matrix Polynomial Integer

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..]

[[2, 3, 4, 5, 6, 7, 8, 9, 10, 11,…],[3 2, 2, 5 2, 3, 7 2, 4, 9 2, 5, 11 2, 6,…],.[4 3, 5 3, 2, 7 3, 8 3, 3, 10 3, 11 3, 4, 13 3,…],[5 4, 3 2, 7 4, 2, 9 4, 5 2, 11 4, 3, 13 4, 7 2,…],[6 5, 7 5, 8 5, 9 5, 2, 11 5, 12 5, 13 5, 14 5, 3,…],[7 6, 4 3, 3 2, 5 3, 11 6, 2, 13 6, 7 3, 5 2, 8 3,…],[8 7, 9 7, 10 7, 11 7, 12 7, 13 7, 2, 15 7, 16 7, 17 7,…],[9 8, 5 4, 11 8, 3 2, 13 8, 7 4, 15 8, 2, 17 8, 9 4,…],[10 9, 11 9, 4 3, 13 9, 14 9, 5 3, 16 9, 17 9, 2, 19 9,…],.[11 10, 6 5, 13 10, 7 5, 3 2, 8 5, 17 10, 9 5, 19 10, 2,…],…]

Type: Stream Stream Fraction Integer

You can use parallel iteration across lists and streams to create iteration:parallel new lists.

[i/j for i in 3.. by 10 for j in 2..]

[3 2, 13 3, 23 4, 33 5, 43 6, 53 7, 63 8, 73 9, 83 10, 93 11,…]

Type: Stream Fraction Integer

Iteration stops if the end of a list or stream is reached.

[i**j for i in 1..7 for j in 2.. ]

[1, 8, 81, 1024, 15625, 279936, 5764801]

Type: Stream Integer

As with loops, you can combine these modifiers to make very complicated conditions.

[ [ [i,j] for i in 10..15 | prime? i] for j in 17..22 | j = squareFreePart j]

[[[11, 17],[13, 17]],[[11, 19],[13, 19]],[[11, 21],[13, 21]],[[11, 22],[13, 22]]]

Type: List List List PositiveInteger

See List (section ListXmpPage ) and Stream (section StreamXmpPage ) for more information on creating and manipulating lists and streams, respectively.

Book Contents Previous Section 5.4 Loops Next Section 5.6 An Example: Streams of Primes Book Index