Section5.2

]>
Book Contents Previous Section 5.1 Immediate and Delayed Assignments Next Section 5.3 if-then-else Book Index

## 5.2 Blocks

A block is a sequence of expressions evaluated in the order that they appear, except as modified by control expressions such as break, break return, return iterate and iterate if-then-else constructions. The value of a block is the value of the expression last evaluated in the block.

To leave a block early, use ``=>''. For example, i<0=>x. The expression before the ``=>'' must evaluate to true or false. The expression following the ``=>'' is the return value for the block.

A block can be constructed in two ways:

1. the expressions can be separated by semicolons and the resulting expression surrounded by parentheses, and
2. the expressions can be written on succeeding lines with each line indented the same number of spaces (which must be greater than zero). indentation A block entered in this form is called a pile.

Only the first form is available if you are entering expressions directly to Axiom. Both forms are available in .input files.

The syntax for a simple block of expressions entered interactively is

( expression 1; expression 2; ...; expression N )

The value returned by a block is the value of an => expression, or expression N if no => is encountered.

In .input files, blocks can also be written using piles. The examples throughout this book are assumed to come from .input files.

In this example, we assign a rational number to a using a block consisting of three expressions. This block is written as a pile. Each expression in the pile has the same indentation, in this case two spaces to the right of the first line.

a :=
i := gcd(234,672)
i := 3*i**5 - i + 1
1 / i

1 23323

Type: Fraction Integer

Here is the same block written on one line. This is how you are required to enter it at the input prompt.

a := (i := gcd(234,672); i := 3*i**5 - i + 1; 1 / i)

1 23323

Type: Fraction Integer

Blocks can be used to put several expressions on one line. The value returned is that of the last expression.

(a := 1; b := 2; c := 3; [a,b,c])

[1, 2, 3]

Type: List PositiveInteger

Axiom gives you two ways of writing a block and the preferred way in an .input file is to use a pile. file:input Roughly speaking, a pile is a block whose constituent expressions are indented the same amount. You begin a pile by starting a new line for the first expression, indenting it to the right of the previous line. You then enter the second expression on a new line, vertically aligning it with the first line. And so on. If you need to enter an inner pile, further indent its lines to the right of the outer pile. Axiom knows where a pile ends. It ends when a subsequent line is indented to the left of the pile or the end of the file.

Blocks can be used to perform several steps before an assignment (immediate or delayed) is made.

d :=
c := a**2 + b**2
sqrt(c * 1.3)

2. 5495097567 96392415

Type: Float

Blocks can be used in the arguments to functions. (Here h is assigned 2. 1+3. 5.)

h := 2.1 +
1.0
3.5

5. 6

Type: Float

Here the second argument to eval is x=z, where the value of z is computed in the first line of the block starting on the second line.

eval(x**2 - x*y**2,
z := %pi/2.0 - exp(4.1)
x = z
)

58. 7694912705 67072878 y 2+3453. 8531042012 59382

Type: Polynomial Float

Blocks can be used in the clauses of if-then-else expressions (see ugLangIf ).

if h > 3.1 then 1.0 else (z := cos(h); max(z,0.5))

1. 0

Type: Float

This is the pile version of the last block.

if h > 3.1 then
1.0
else
z := cos(h)
max(z,0.5)

1. 0

Type: Float

Blocks can be nested.

a := (b := factorial(12); c := (d := eulerPhi(22); factorial(d));b+c)

482630400

Type: PositiveInteger

This is the pile version of the last block.

a :=
b := factorial(12)
c :=
d := eulerPhi(22)
factorial(d)
b+c

482630400

Type: PositiveInteger

Since c+d does equal 3628855, a has the value of c and the last line is never evaluated.

a :=
c := factorial 10
d := fibonacci 10
c + d = 3628855 => c
d

3628800

Type: PositiveInteger
Book Contents Previous Section 5.1 Immediate and Delayed Assignments Next Section 5.3 if-then-else Book Index