From: Mike Eggleston
Subject: Re: backquote a symbol?
Date: 
Message-ID: <slrn9onvu0.jk3.mikee@kensho.eggtech.com>
On 28 Aug 2001 12:40:54 -0700, Will Fitzgerald <··········@inetmi.com> wrote:
> ·····@kensho.eggtech.com wrote in message news:<····················@kensho.eggtech.com>...
>> On Mon, 27 Aug 2001 21:45:06 GMT, <······@spacy.Boston.MA.US> wrote:
>> >>>>>> On Mon, 27 Aug 2001 16:08:14 GMT, Mike Eggleston ("Mike") writes:
>> >  Mike> I'm playing with lisp some and am trying to make a macro.
>> > 
>> >  Mike> What I want is to hand a symbol to a function and have
>> >  Mike> that function print a readable form of the symbol out to
>> >  Mike> stdout or a stream. I'm not looking for the open file
>> >  Mike> symantics just yet, only the macro.
>> > 
>> > Mike,
>> > 
>> > You're trying to make this a lot more complicated than necessary.  
>> > You don't need a macro do this.  Symbols are an ordinary type of data,
>> > and Lisp functions can manipulate them as easily as any other thing.
>> > A symbol (like most anything else) can be output to a file with PRINT,
>> > and input with READ.
>> > 
>> > Here is a function that does what you are asking.  I have included 
>> > the file stream protocol in it, because otherwise there would be
>> > nothing to the function --  all it does is call PRINT!
>> > 
>> > (defun psexp (sym file)
>> >    (with-open-file (o file :direction :output)
>> >      (print sym o)))
>> > 
>> > The corresponding input function simply needs to call READ.
>> > (In a real program, you might be concerned with controlling
>> > certain parameters of READ, but you can ignore all that for now.)
>> 
>> <snip>
>> 
>> > This doesn't make any sense, and looks like you are somewhat confused
>> > and mixed up about many basic elements of Lisp, including functions,
>> > symbols, variables, and quoting.  Rather than trying to explain
>> > everything all at once, I'll let you consider what I've already
>> > written and ask more questions from that point.
>> > 
>> > Chris
>> 
>> I tried with the (print), but am looking for something that can
>> receive a symbol and print the full (setq) line for that symbol
>> so that a (read) and (eval) reloads the symbol definition into core.
>> 
>> Mike
> 
> 
> I think you want:
> 
> (defmacro psexp (variable) 
>  `'(SETQ ,variable ',(eval variable)))

That was it, thanks.
From: Kent M Pitman
Subject: Re: backquote a symbol?
Date: 
Message-ID: <sfwzo8j6g91.fsf@world.std.com>
[ replying to comp.lang.lisp only
  http://world.std.com/~pitman/pfaq/cross-posting.html

  I know this had a follow-up to of gnu.emacs.lisp, but I don't post there
  and the question was posted in the newsgroup I read, which was
  comp.lang.lisp, so that's where I reply.]

·····@kensho.eggtech.com (Mike Eggleston) writes:

> On 28 Aug 2001 12:40:54 -0700, Will Fitzgerald <··········@inetmi.com> wrote:
> ...
> > I think you want:
> > 
> > (defmacro psexp (variable) 
> >  `'(SETQ ,variable ',(eval variable)))
> 
> That was it, thanks.

Uh, this will evaluate variable at compile time, not at load time.
That's probably going to do the wrong thing.

Further, this doesn't print anything.  It just returns it.
If you must use a macro, You want one of these:

 ;; considers variable in the lexical environment, too...
 (defmacro psexp (variable)
   `(print `(setq ,',variable ',,variable)))

 OR, equivalently:

 (defmacro psexp (variable)
   `(print (list 'setq ',variable `',,variable)))

 OR, using the dynamic environment but not the lexical environment:

 (defmacro psexp (variable)
   `(print `(setq ,',variable ',(symbol-value ',variable))))

 OR, equivalently,

 (defmacro psexp (variable)
   `(print (list 'setq ,variable 
                 (list 'quote (symbol-value ',variable)))))

But really, as suggested earlier, you're better off with a function.

 (defun psexp (variable)
   (print `(setq ,variable ,(symbol-value variable))))

I used SYMBOL-VALUE rather than EVAL since it's more lightweight and seems
to fit the need here.  It's not quite the same if you're using global symbol
macros, but I sort of assume you're not since you didn't say you were.