XLISP macrolet

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference

# macrolet

Type: | - | special form (fsubr)
Source: | - | xlcont.c

## Syntax

(macrolet ([macro ... ]) expr ... )
  macro - a macro definition binding which is of the form:
    (symbol arg-list body)
    symbol - the symbol specifying the macro name
    arg-list - the argument list for the macro
    body - the body of the macro
  expr - an expression
  returns - the value of the last expression

## Description

The 'macrolet' special form is basically a local block construct that allows local 'macro' definitions followed by a block of code to evaluate. The first form after the macrolet is the 'binding' form. It contains a series of 'macros'. The 'macrolet' form will sequentially execute the 'exprs' after defining the 'macros'. The value of the last 'expr' evaluated is returned. When the 'macrolet' is finished execution, the 'symbols' that were defined will no longer exist.

## Examples

    > (macrolet ((pls (n1 n2)     ; MACROLET defining a PLS macro
                   `(+ ,n1 ,n2)))
        (pls 4 5))
    9

    > (pls 4 5)                   ; the PLS macro no longer exists
    error: unbound function - PLS

    > (macrolet ()                ; an empty MACROLET
        (print 'a))
    A  ; screen output of PRINT
    A  ; return value

## Known Problems

1. In XLISP, only macros defined by defmacro [interned in the *obarray*] can be used with setf:

    (setq a #(1 2 3))

    (defmacro second-array-element (array)
      `(aref ,array 1))

    (second-array-element a)            => 2
    (setf (second-array-element a) 'x)  => X
    a                                   => #(1 X 3)

With macros defined by 'macrolet' [stored in the lexical environment], setf signals a 'bad place form' error:

    (macrolet ((second-element (array)
                 `(aref ,array 1)))
      (second-element a))               => X

    (macrolet ((second-element (array)
                 `(aref ,array 1)))
      (setf (second-element a) 'y))     => error: bad place form

2. In XLISP, the macroexpand and macroexpand-1 functions can only expand macros defined by defmacro:

    > (macroexpand-1 '(second-array-element a))
    (AREF A 1)

With macros defined by 'macrolet', the macro form is returned unexpanded:

    > (macrolet ((second-element (array)
                   `(aref ,array 1)))
        (macroexpand-1 '(second-element a)))
    (SECOND-ELEMENT A)

In XLISP, the macroexpand and macroexpand-1 functions only search in the *obarray* for defined macros, but not in the lexical environment.

Back to Top

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference