From: Kevin T. Ryan
Subject: Is it possible to define a function w/in a macro as (defun some-fn-name-,param)?
Date: 
Message-ID: <IOidnc2xOphd9U3enZ2dnUVZ_s2dnZ2d@comcast.com>
Hi All -

I hope this isn't a dumb question, b/c I'm totally new to lisp (trying 
Practical Common Lisp out).  I've defined a macro as such:

(defmacro my-macro (fn-num)
   `(defun my-fn-,fn-num ()
      (format t "~d " ,fn-num)))

What I'm trying to do is create a macro that takes a number as a param 
and then creates a function based on that number.  I actually wrote an 
original macro as:

(defmacro my-macro (fn-name fn-num)
   `(defun ,fn-name ()
      (format t "~d " ,fn-num)))

and that one works w/out a hitch.  But then I was calling it as:

(my-macro macro-attempt-3 3)

So I found myself writing the 3 more times than really necessary, so I 
wanted to eliminate the extra typing but it doesn't work.  Obviously not 
a big deal, but I was really just wondering if it was possible.

TIA,

Kevin

From: Coby Beck
Subject: Re: Is it possible to define a function w/in a macro as (defun some-fn-name-,param)?
Date: 
Message-ID: <R7_zf.111360$km.66264@edtnps89>
"Kevin T. Ryan" <···········@yahoo.com> wrote in message 
·····································@comcast.com...
> Hi All -
>
> I hope this isn't a dumb question, b/c I'm totally new to lisp (trying 
> Practical Common Lisp out).  I've defined a macro as such:
>
> (defmacro my-macro (fn-num)
>   `(defun my-fn-,fn-num ()
>      (format t "~d " ,fn-num)))
>
> What I'm trying to do is create a macro that takes a number as a param and 
> then creates a function based on that number.  I actually wrote an 
> original macro as:

This will do what you want:

(defmacro my-macro (fn-num)
  `(defun ,(intern (format nil "MY-FN-~A" fn-num)) ()
     (format t "~d " ,fn-num)))

I understand you are just learning lisp, but if you are solving a problem of 
some more concrete kind, you may be better with a list of lambda exressions 
that you can retrieve by an index or some such.  Usually that is the better 
answer when you find yourself wanting to name functions in the way you are.

Anyway, you have the short answer above!

Cheers,

-- 
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Stephen Compall
Subject: Re: Is it possible to define a function w/in a macro as (defun some-fn-name-,param)?
Date: 
Message-ID: <pan.2006.01.20.05.12.44.362972@nocandysw.com>
On Thu, 19 Jan 2006 23:36:03 -0500, Kevin T. Ryan wrote:
> (defmacro my-macro (fn-num)
>    `(defun my-fn-,fn-num ()
>       (format t "~d " ,fn-num)))

The key to understanding this is understanding that ` backquote defines a
structural pattern expansion, not a textual one.  That is, my-fn- and
fn-num are separate list elements just as they would be if you put ()
between them.

Here's the equivalent Lisp code [1] to your above backquote, that is,
everything in `(defun ...):

(list 'defun 'my-fn- fn-num '()
      (list 'format 't '"~d " fn-num))

To solve this problem, consider that the , is not a variable substitution
so much as an "unquote" that says "evaluate the following form", which can
of course be an arbitrary Lisp expression.  You will have to create a
string containing your function's complete name (perhaps with FORMAT with
NIL as the first argument), and pass the result to INTERN.

[1] Equivalent is quite literal; I got it by quoting your backquoted form
and calling macroexpand-1 on it.  The exact form may vary, as it isn't
specified by ANSI CL.

-- 
Stephen Compall
Email: My username is s11.  My domain is member..org, but insert the
abbreviation for `Free Software Foundation' between the dots.
From: ············@gmail.com
Subject: Re: Is it possible to define a function w/in a macro as (defun some-fn-name-,param)?
Date: 
Message-ID: <1137865182.981466.68690@g47g2000cwa.googlegroups.com>
Stephen Compall wrote:
> On Thu, 19 Jan 2006 23:36:03 -0500, Kevin T. Ryan wrote:
> > (defmacro my-macro (fn-num)
> >    `(defun my-fn-,fn-num ()
> >       (format t "~d " ,fn-num)))
>
> The key to understanding this is understanding that ` backquote defines a
> structural pattern expansion, not a textual one.  That is, my-fn- and
> fn-num are separate list elements just as they would be if you put ()
> between them.
>
> Here's the equivalent Lisp code [1] to your above backquote, that is,
> everything in `(defun ...):
>
> (list 'defun 'my-fn- fn-num '()
>       (list 'format 't '"~d " fn-num))
>
> To solve this problem, consider that the , is not a variable substitution
> so much as an "unquote" that says "evaluate the following form", which can
> of course be an arbitrary Lisp expression.  You will have to create a
> string containing your function's complete name (perhaps with FORMAT with
> NIL as the first argument), and pass the result to INTERN.
>
> [1] Equivalent is quite literal; I got it by quoting your backquoted form
> and calling macroexpand-1 on it.  The exact form may vary, as it isn't
> specified by ANSI CL.
>
> --
> Stephen Compall
> Email: My username is s11.  My domain is member..org, but insert the
> abbreviation for `Free Software Foundation' between the dots.

Thanks so much to both of you!  I'll be looking into using lists to
perhaps gather the various functions instead of my ill-formed naming
conventions (too much procedural thought process I guess ;).