Hi. I'm having a problem that I'm not sure if it is a common
lisp caveat or a implementation specific problem. I'm using CLISP for
now to test it. Let's see what happens:
Take this piece of code:
--
(defun doit (l)
(setf (seg l) (1+ (seg l)))
l)
(defmacro seg (l)
`(second ,l))
--
After running it into CLISP, gives:
--
> (load "test.lsp")
;; Loading file test.lsp ...
;; Loading of file test.lsp is finished.
T
> (doit '(1 2 3))
*** - EVAL: undefined function (SETF SEG)
1. Break> backtrace
EVAL frame for form ((SETF SEG) (1+ (SEG L)) #:G146)
EVAL frame for form (LET* ((#:G146 L)) ((SETF SEG) (1+ (SEG L))
#:G146))
EVAL frame for form (PROGN (LET* ((#:G146 L)) ((SETF SEG) (1+ (SEG L))
#:G146)) L)
APPLY frame for call (DOIT '(1 2 3))
EVAL frame for form (DOIT '(1 2 3))
--
But if I exchange the macro and the defun:
--
(defmacro seg (l)
`(second ,l))
(defun doit (l)
(setf (seg l) (1+ (seg l)))
l)
--
It works fine:
--
> (load "test.lsp")
;; Loading file test.lsp ...
;; Loading of file test.lsp is finished.
T
> (doit '(1 2 3))
(1 3 3)
--
In each experiment, a brand new clisp was launched. Why does this
happen? The only thing that comes into my mind is this: defun is not
as clean as I supposed it was, as when a new function is defined, the
function name are assumed to be functions, not macros. Defun is thus
not as declarative as desired. It is a declarative expression in some
situations, and not in others.
Regards,
In article <··············@isr.isr.ist.utl.pt>,
Rodrigo Ventura (Yoda) <·····@isr.ist.utl.pt> wrote:
>In each experiment, a brand new clisp was launched. Why does this
>happen? The only thing that comes into my mind is this: defun is not
>as clean as I supposed it was, as when a new function is defined, the
>function name are assumed to be functions, not macros. Defun is thus
>not as declarative as desired. It is a declarative expression in some
>situations, and not in others.
Common Lisp is not a declarative language, it's an imperative language.
Since macros are expanded at compile time (more correctly, at semantic
analysis time), they have to be defined before they are used. See section
25.1.3:
macros referenced in the code being compiled must have been previously
defined in the compile-time environment. The compiler must treat as a
function call any form that is a list whose car is a symbol that does
not name a macro or special form. (This implies that SETF methods must
also be available at compile time.)
--
Barry Margolin, ······@bbnplanet.com
BBN Corporation, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>