macro

Gamma: Version 4.0
Prev |  | Next

# `macro`

`macro` -- helps generate custom functions.

## Syntax

    macro name (args) statement

## Arguments

`name`

  The name of the macro.

`args`

  An argument list.

`statement`

  The body of the macro.

## Returns

A named macro definition in Lisp syntax.

## Description

This function lets Gamma generate custom functions. The most common type of macro is one that will call different functions for different kinds of arguments. Once the macro has been called on a specific kind of argument, successive calls to the macro for that kind of argument will not be processed by the macro at all, but will be handed straight over to its corresponding function.

One advantage is speed, as the macro code is only executed once. Thereafter only the corresponding function code is executed.

## Example

1. Define a macro. This macro checks its arguments to see if they are symbols or strings, and performs correspondingly different operations on them.

        macro symbol_number (!x,!y)
        {
           if (symbol_p(x) && symbol_p(y))
              string(string(x),string(y));
           else if (symbol_p(x) && number_p(y))
              `setq(@x,@y);
           else if (number_p(x) && symbol_p(y))
              `setq(@y,@x);
           else if (number_p(x) && number_p(y))
              y + x;
           else
              error(string("Error: I accept only symbols and numbers."));
        }

2. Calling the macro gives these results:

        Gamma> symbol_number(st,art);
        "start"
        Gamma> symbol_number(myvalue,35);
        35
        Gamma> myvalue;
        35
        Gamma> symbol_number(40,yourvalue);
        40
        Gamma> yourvalue;
        40
        Gamma> symbol_number(35,40);
        75
        Gamma>

3. Define a function that includes the macro, and then call that function.

        Gamma> function f(x,y) {symbol_number(b,3);}
        (defun f (x y) (symbol_number b 3))
        Gamma> f(#r,7);
        3
        Gamma>

4. Check the function definition. Note that the macro code is now gone. In its place is `setq`, the function it calls for the specified kind of argument.

        Gamma> f;
        (defun f (x y) (setq b 3))
        Gamma>

## See Also

`function`

Prev | Home | Next
`ivar_type` | Up | `new`

Copyright 1995-2002 by Cogent Real-Time Systems, Inc.