]>
Book Contents Previous Section 6.1 Functions vs. Macros Next Section 6.3 Introduction to Functions Book Index

## 6.2 Macros

A macro provides general textual substitution of macro an Axiom expression for a name. You can think of a macro as being a generalized abbreviation. You can only have one macro in your workspace with a given name, no matter how many arguments it has.

The two general forms for macros are

macro name == body
macro name(arg1,...) == body

where the body of the macro can be any Axiom expression.

For example, suppose you decided that you like to use df for D. You define the macro df like this.

macro df == D
Type: Void

Whenever you type df, the system expands it to D.

df(x**2 + x + 1,x)

2 x+1

Type: Polynomial Integer

Macros can be parameterized and so can be used for many different kinds of objects.

macro ff(x) == x**2 + 1
Type: Void

Apply it to a number, a symbol, or an expression.

ff z

z 2+1

Type: Polynomial Integer

Macros can also be nested, but you get an error message if you run out of space because of an infinite nesting loop.

macro gg(x) == ff(2*x - 2/3)
Type: Void

This new macro is fine as it does not produce a loop.

gg(1/w)

13 w 2-24 w+36 9 w 2

Type: Fraction Polynomial Integer

This, however, loops since gg is defined in terms of ff.

macro ff(x) == gg(-x)
Type: Void

The body of a macro can be a block.

macro next == (past := present; present := future; future := past + present)
Type: Void

Before entering next, we need values for present and future.

present : Integer := 0

0

Type: Integer
future : Integer := 1

1

Type: Integer

Repeatedly evaluating next produces the next Fibonacci number.

next

1

Type: Integer

And the next one.

next

2

Type: Integer

Here is the infinite stream of the rest of the Fibonacci numbers.

[next for i in 1..]

[3,5,8,13,21,34,55,89,144,233,…]

Type: Stream Integer

Bundle all the above lines into a single macro.

macro fibStream ==
present : Integer := 1
future : Integer := 1
[next for i in 1..] where
macro next ==
past := present
present := future
future := past + present
Type: Void

Use concat concat Stream to start with the first two Fibonacci numbers Fibonacci numbers.

concat([0,1],fibStream)

[0,1,2,3,5,8,13,21,34,55,…]

Type: Stream Integer

The library operation fibonacci is an easier way to compute these numbers.

[fibonacci i for i in 1..]

[1,1,2,3,5,8,13,21,34,55,…]

Type: Stream Integer
Book Contents Previous Section 6.1 Functions vs. Macros Next Section 6.3 Introduction to Functions Book Index