[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [spr7631] Defining a Macro



[This matter has been assigned the tracking identifier "spr7631".
Please refer to it in any followup communication.  Also, be sure
to cc bugs@franz.com so that if I am unavailable someone else will
be able to respond.]

   From: "Abdel Kader Diagne" <diagne@dfki.uni-sb.de>
   To: allegro-cl@ucbvax.berkeley.edu
   Date: Tue, 09 Feb 93 10:38:20 +0100

   I am trying to define a macro to construct expressions like

	'(fcn-1 [arg1-val [arg2-val]]) 

   where arg1-val and arg2-val are (the values of) the arguments of the macro.

   I think, the usual way to do it is defining a macro like

   (defmacro BUILD-MY-EXPRESSION (arg1 arg2)
     `'(fcn-1 [,arg1 [,arg2]]))

   But that doesn't work: the evaluation of the form 
    (BUILD-MY-EXPRESSION 'test1 'test2) returns (FCN-1 [,ARG1 [,ARG2]]) instead of
     (FCN-1 [TEST1 [TEST2]]).

   I suppose it is due to the fact that #\[ is a macro-character associated to a
   specific function. If so, is there a way to use it locally as a usual char?

   Reformulation of the problem:
     I would like to define a macro that returns the same expression as the function 
     defined below:

   (defun BUILD-MY-EXPRESSION (arg1 arg2)
     (let ((my-expr 
	    (format nil "(fcn-1 [~a [~a]])"arg1 arg2)))
       my-expr))

    Any clue ?

It's not really clear from your message what you want your macro to
do.  The "square bracket" characters `[' and `]' do not have any
special meaning in Common Lisp reader syntax -- they are just like
alphabetic characters such as `A' and `Z'.  Since the brackets don't
mean anything, it's not clear what you mean by

    expressions like '(fcn-1 [arg1-val [arg2-val]])

Your later example

    (format nil "(fcn-1 [~a [~a]])"arg1 arg2)

suggests that you intend the macro to construct a string, but this
otherwise seems unlikely.

Part of the confusion here may be between lisp expressions which are
trees of conses and the representation of lisp expressions as
characters (e.g. in a file) that are input and output by the lisp
reader and printer.  These are very different things, of course, like
the difference between a horse and a picture of a horse.  When one
reads something like this in a lisp manual

   (fcn arg1 [arg2])

it means that fcn can be called with either one or two arguments.  But
any _particular_ form that is a call to fcn will be represented as a
2- or 3-element list.  The metasyntax using square brackets to
represent an optional syntactic component never actually appears in a
program.

The purpose of a lisp macro is to substitute one executable lisp
subform for another.  All three of the following are potentially valid
lisp forms:

   (fcn-1)

which is a list of length 1, probably representing a zero-argument
call to the function fcn-1;

   (fcn-1 'test1 'test2)

which is a list of length 3, probably representing a two-argument call
to the function fcn-1;

   "(fcn-1 ['test1 [test2]])"

which is a string of length 24 and which evaluates to itself (i.e. the
same sting) when evaluated.

Which kind of thing is it that you want your macro to substitute into
your code?