Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
defmacro question
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  13 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
John Clonts  
View profile  
 More options Jul 25 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: John Clonts <joh...@my-deja.com>
Date: 2000/07/25
Subject: defmacro question
I am trying to make a macro which will defun a function whose name is
constructed from a macro parameter, e.g.

(define-struct person (name))
--> defuns a function called make-person

(make-person "joe")

Here is my attempt so far:

(defmacro define-struct (nm)
  ; (defun make-'nm' () "hello")
  (list
    'defun
    (symbol-function
       (make-symbol (stringappend "make-" (symbol-name nm))))
    '()
    "hello"))

(defun stringappend (&rest lst)
   (apply #'concatenate 'string "" lst))

But the problem for now is that the (symbol-function ....) expression
result is apparently not suitable for the defun.

Perhaps a related confusion is:  What is the difference between 'foo and
:foo

(eq (type-of 'foo)(type-of :foo)) ==> T
(eq 'foo :foo) ==>NIL
(eql 'foo :foo) ==>NIL
(equal 'foo :foo) ==>NIL

(I think I understand that (eq #'foo (symbol-function 'foo)))

(As you can imagine I am coming from Scheme.  I am using at the moment
Corman Lisp.

Thank you for any help you can offer :)

John

Sent via Deja.com http://www.deja.com/
Before you buy.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Clonts  
View profile  
 More options Jul 25 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: John Clonts <jclo...@mastnet.net>
Date: 2000/07/25
Subject: Re: defmacro question

Thank you very much!  It was just the 'intern' that I was missing.  I
will try and figure out why (make-symbol) makes something different than
(intern) and thats also different than just 'symbol, and of cours
#'func-symbol.  (whew!)

Cheers,
John


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Clonts  
View profile  
 More options Jul 25 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: John Clonts <jclo...@mastnet.net>
Date: 2000/07/25
Subject: Re: defmacro question

Thanks, but all that is beside the point in my case-- I'm just trying to
figure how the macros work.

> > (make-person "joe")

> > Here is my attempt so far:

> > (defmacro define-struct (nm)
> >   ; (defun make-'nm' () "hello")
> >   (list
> >     'defun
> >     (symbol-function
> >        (make-symbol (stringappend "make-" (symbol-name nm))))
> >     '()
> >     "hello"))

> - you could also use backquote to make your macros more readable

gotcha. 6 vs half-dozen.

> - what are you trying to achieve with calling symbol-function?
> That returns a function object, normally defun is used with a
> symbol as its first parameter.

e.g. if the name passed in is 'balloon, I want the result function to be
called make-balloon.

> - intern might be better than make-symbol in this case

This is the solution I am seeking, thanks! (And to Mr. Joswig)

> - you'll also get interesting results with mixed-case names,
> remember that CL usually converts symbol names to uppercase when
> reading them.

Ok, I'll keep that in mind.

> > Perhaps a related confusion is:  What is the difference between 'foo and
> > :foo

> 'foo -> the symbol FOO in the current package

> :foo -> the symbol FOO in the keyword package

> --

Thanks!

John


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rainer Joswig  
View profile  
 More options Jul 26 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: Rainer Joswig <rainer.jos...@ision.net>
Date: 2000/07/26
Subject: Re: defmacro question
In article <8ll4hf$cb...@nnrp1.deja.com>, John Clonts

(defmacro define-struct (name)
  `(defun ,(intern (concatenate 'string "MAKE-" (symbol-name name)))
          ()
     "hello"))

? (macroexpand-1 '(define-struct foo))

-> (DEFUN MAKE-FOO () "hello")

Or:

(defmacro define-struct (name)
  (list 'defun
        (intern (concatenate 'string "MAKE-" (symbol-name name)))
        '()
        "hello"))

--
Rainer Joswig, BU Partner,
ISION Internet AG, Steinhöft 9, 20459 Hamburg, Germany
Tel: +49 40 3070 2950, Fax: +49 40 3070 2999
Email: mailto:rainer.jos...@ision.net WWW: http://www.ision.net/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martti Halminen  
View profile  
 More options Jul 26 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: Martti Halminen <Martti.Halmi...@solibri.com>
Date: 2000/07/26
Subject: Re: defmacro question

John Clonts wrote:

> I am trying to make a macro which will defun a function whose name is
> constructed from a macro parameter, e.g.

> (define-struct person (name))
> --> defuns a function called make-person

Your choice of names is a little misleading: Common Lisp already
has defstruct, which as one of its effects creates a constructor
function (or was it method? I don't remember offhand) with a
make- -style name, so there is a risk that somebody reading your
code could get mixed up on which function you are talking about.

> (make-person "joe")

> Here is my attempt so far:

> (defmacro define-struct (nm)
>   ; (defun make-'nm' () "hello")
>   (list
>     'defun
>     (symbol-function
>        (make-symbol (stringappend "make-" (symbol-name nm))))
>     '()
>     "hello"))

- you could also use backquote to make your macros more readable

- what are you trying to achieve with calling symbol-function?
That returns a function object, normally defun is used with a
symbol as its first parameter.

- intern might be better than make-symbol in this case

- you'll also get interesting results with mixed-case names,
remember that CL usually converts symbol names to uppercase when
reading them.

> Perhaps a related confusion is:  What is the difference between 'foo and
> :foo

'foo -> the symbol FOO in the current package

:foo -> the symbol FOO in the keyword package

--


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Jul 26 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: Barry Margolin <bar...@genuity.net>
Date: 2000/07/26
Subject: Re: defmacro question
In article <397E51AB.10D59...@mastnet.net>,
John Clonts  <jclo...@mastnet.net> wrote:

>Thank you very much!  It was just the 'intern' that I was missing.  I
>will try and figure out why (make-symbol) makes something different than
>(intern) and thats also different than just 'symbol, and of cours
>#'func-symbol.  (whew!)

MAKE-SYMBOL and INTERN both create symbols.  The latter also interns the
symbol it created; if a symbol isn't interned, you won't be able to access
it by typing its name.

#'func-symbol evaluates to the function that FUNC-SYMBOL is bound to.
Since DEFUN requires its first parameter to be a function name, not a
function object, it makes no sense to put that in the macro expansion.

--
Barry Margolin, bar...@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas A. Russ  
View profile  
 More options Jul 26 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: t...@sevak.isi.edu (Thomas A. Russ)
Date: 2000/07/26
Subject: Re: defmacro question

Correct in your analysis.  Defun wants to have a symbol as the second
element of its list.  Defun itself takes care of setting the
SYMBOL-FUNCTION slot of the symbol, so in normal everyday Common Lisp
work you don't actually use this.  (Nor SYMBOL-VALUE either).  I realize
that coming from Scheme having the separate value and function
namespaces may be a little more complicated, but it's not as bad as you
are making it.

Anyway, you most likely don't want to use MAKE-SYMBOL, since that
creates an uninterned symbol.  You want to use INTERN instead.  The
problem with an uninterned symbol is that you can't reference it by
name, only by keeping a binding to it.  INTERN creates a symbol (if
necessary) and stores an association between the symbol name and the
symbol object.  This association is stored in a data structure called a
package (of which there are several in lisp -- see below for an example)
so that the reader can map symbol names to symbol objects.

Another issue to be aware of is that the reader normally converts input
into uppercase:

  (symbol-name 'foo) =>  "FOO"

but make-symbol and intern do not:

  (symbol-name (intern "foo"))  => "foo"

so if you concatenate the string "make-" to the symbol name of 'FOO you
would end up with a symbol that will most likely print as:

  |make-FOO|

which is distinct from the symbol 'make-foo (which is the same as the
symbol 'MAKE-FOO).  You would then need to reference your created
function as:

  (|make-FOO| ...)

Of course, if you uppercase the string, then it wouldn't be a problem.

What can also help you in debugging macros is the use of the MACROEXPAND
and MACROEXPAND-1 functions, which will show you what the macro expands
into.  Together with the pretty printer PPRINT, you can see what the
system is doing.

BTW, if you want to get serious about writing macros, it would save you
lots of effort and frustration to learn the backquote syntax.  This
allows a much clearer (to me at least) view of what kinds of structures
you are creating, since you don't have all those calls to LIST
cluttering things up.

Normally the way I write a macro is to start with the end result, namely
the form I want to have the macro produce, and then I backquote it and
then add commas to the variable parts of the structure.  (Then I worry
about variable capture and multiple evaluation of arguments and fix
those up).

> Perhaps a related confusion is:  What is the difference between 'foo and
> :foo

Try:

  (symbol-package 'foo)
  (symbol-package :foo)

--
Thomas A. Russ,  USC/Information Sciences Institute          t...@isi.edu    


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Jul 26 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.net>
Date: 2000/07/26
Subject: Re: defmacro question
* Rainer Joswig <rainer.jos...@ision.net>
| (defmacro define-struct (name)
|   `(defun ,(intern (concatenate 'string "MAKE-" (symbol-name name)))
|           ()
|      "hello"))

  To avoid exposing the case-ness of symbols, I prefer (symbol-name 'make-).

#:Erik
--
  If this is not what you expected, please alter your expectations.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rainer Joswig  
View profile  
 More options Jul 26 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: Rainer Joswig <rainer.jos...@ision.net>
Date: 2000/07/26
Subject: Re: defmacro question
In article <3173614228918...@naggum.net>, Erik Naggum <e...@naggum.net>
wrote:

> * Rainer Joswig <rainer.jos...@ision.net>
> | (defmacro define-struct (name)
> |   `(defun ,(intern (concatenate 'string "MAKE-" (symbol-name name)))
> |           ()
> |      "hello"))

>   To avoid exposing the case-ness of symbols, I prefer (symbol-name 'make-).

One can use it that way - usually I use strings, because I don't
want to create arbitrary symbols in a package. For example
I'll always write (in-package "FOO") and never
(in-package 'foo). I also really *want* to specify the case-ness
of a symbol.

--
Rainer Joswig, BU Partner,
ISION Internet AG, Steinhöft 9, 20459 Hamburg, Germany
Tel: +49 40 3070 2950, Fax: +49 40 3070 2999
Email: mailto:rainer.jos...@ision.net WWW: http://www.ision.net/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Jul 26 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.net>
Date: 2000/07/26
Subject: Re: defmacro question
* Rainer Joswig <rainer.jos...@ision.net>
| One can use it that way - usually I use strings, because I don't
| want to create arbitrary symbols in a package.

  You can limit the effect to compile-time with #.(symbol-name 'x).

| For example I'll always write (in-package "FOO") and never
| (in-package 'foo).

  I use (in-package :foo).

| I also really *want* to specify the case-ness of a symbol.

  I have a cat, so I know that when she digs her very sharp claws into
  my chest or stomach it's really a sign of affection, but I don't see
  any reason for programming languages to show affection with pain.

#:Erik
--
  If this is not what you expected, please alter your expectations.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven M. Haflich  
View profile  
 More options Jul 26 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: "Steven M. Haflich" <hafl...@pacbell.net>
Date: 2000/07/26
Subject: Re: defmacro question

Erik Naggum wrote:

> * Rainer Joswig <rainer.jos...@ision.net>
> | One can use it that way - usually I use strings, because I don't
> | want to create arbitrary symbols in a package.

>   You can limit the effect to compile-time with #.(symbol-name 'x).

> | For example I'll always write (in-package "FOO") and never
> | (in-package 'foo).

>   I use (in-package :foo).

You can get both benefits this way:

  (in-package #:foo)

It is tedious, but less tedious than the compile-time symbol-name.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Jul 27 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.net>
Date: 2000/07/27
Subject: Re: defmacro question
* "Steven M. Haflich" <hafl...@pacbell.net>
| It is tedious, but less tedious than the compile-time symbol-name.

  concatenate doesn't accept string designators.  in-package does.

#:Erik
--
  If this is not what you expected, please alter your expectations.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kent M Pitman  
View profile  
 More options Jul 28 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: Kent M Pitman <pit...@world.std.com>
Date: 2000/07/28
Subject: Re: defmacro question
"Steven M. Haflich" <hafl...@pacbell.net> writes:

(in-package #:foo) has the problem that #:foo does not self-evaluate.
The reason people like "FOO" and :FOO is that they are cltl1-compatible.

Personally, I always use

  (in-package #.(load-time-value (setf (symbol-value '#1=#:foo) '#1#)))

because it has the benefits of Erik's version and smh's version combined.
That is, it both doesn't expose symbol case and it's backward compatible
to cltl1 and it gets gc'd quickly.

Heh.  

Just kidding.

I rather like (in-package "FOO") because I think it's good for people to
be reminded periodically that package names are conventionally uppercase,
and also because I like the idea that literal constants stand out from code.
But that's probably just because I'm getting old and haven't grown up on
full-color text editors to show me distinctions between code and constants.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »