Greetings,
I was wondering : is it possible to write a macro that changes a symbol
x to something like 'x2', for example :
(change 'a)
a2
>>
... any hints ? this is not for any homeworks, just for my personal
curiosity ... I'm sure there is some function that I should call, just
don't know which
Thanks,
Andrei
Stormbringer <·······@mail.dntis.ro> wrote:
+---------------
| I was wondering : is it possible to write a macro that changes a symbol
| x to something like 'x2', for example :
| (change 'a)
| a2
| >>
| ... any hints ?
+---------------
Hints (Socratic questions, really):
- What are the [externally-visible] constituents of a Common Lisp symbol?
What are the standard accessors for those constituents?
[Further hint: Look in the CLHS for all names beginning with "SYMBOL-".]
- Given the values of the constituents of a symbol, what exactly
would you want done to each one of them to transform it from the
original value into whatever it is you want your "change" to return?
- Given the values of some symbol constituents, how do you find/create
a [potentially new] symbol with those constituents?
- Having researched all the above in the CLHS, how would you package
it all up in one operation? And does it really have to be a macro?
[Further hint: No.]
-Rob
-----
Rob Warnock <····@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607
Rob Warnock wrote:
> Stormbringer <·······@mail.dntis.ro> wrote:
> +---------------
> | I was wondering : is it possible to write a macro that changes a
symbol
> | x to something like 'x2', for example :
> | (change 'a)
> | a2
> | >>
> | ... any hints ?
> +---------------
>
> Hints (Socratic questions, really):
>
> - What are the [externally-visible] constituents of a Common Lisp
symbol?
> What are the standard accessors for those constituents?
> [Further hint: Look in the CLHS for all names beginning with
"SYMBOL-".]
>
> - Given the values of the constituents of a symbol, what exactly
> would you want done to each one of them to transform it from the
> original value into whatever it is you want your "change" to
return?
>
> - Given the values of some symbol constituents, how do you
find/create
> a [potentially new] symbol with those constituents?
>
> - Having researched all the above in the CLHS, how would you package
> it all up in one operation? And does it really have to be a macro?
> [Further hint: No.]
Here is my concoction:
(defun new-sym(x)
(intern (string-upcase (concatenate 'string (symbol-name x) "2"))))
> (new-sym 'x) => X2 NIL
> (new-sym 'y) => Y2 NIL
Seems to work as I wanted and I realized in the process that what I
really want is (make-symbol <x as string>), i.e. a symbol name
different from x.
Thanks for the hints!
Regards,
Andrei
In article <························@z14g2000cwz.googlegroups.com>,
"Stormbringer" <·······@mail.dntis.ro> wrote:
> Here is my concoction:
>
> (defun new-sym(x)
> (intern (string-upcase (concatenate 'string (symbol-name x) "2"))))
Why do you need the STRING-UPCASE? If the original symbol went through
the normal reading process, it will already have been upcased. And if
the original symbol was entered with something like |x|, they obviously
*wanted* it to be lowercase, so why should the new one be different?
--
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Barry Margolin wrote:
> In article <························@z14g2000cwz.googlegroups.com>,
> "Stormbringer" <·······@mail.dntis.ro> wrote:
>
> > Here is my concoction:
> >
> > (defun new-sym(x)
> > (intern (string-upcase (concatenate 'string (symbol-name x)
"2"))))
>
> Why do you need the STRING-UPCASE? If the original symbol went
through
> the normal reading process, it will already have been upcased. And
if
> the original symbol was entered with something like |x|, they
obviously
> *wanted* it to be lowercase, so why should the new one be different?
This is what I thought too but in practice here is what I get under
Allegro CL Lite 5.0.1 :
> (intern (concatenate 'string "x" "2")) => |x2|
> (intern (string-upcase (concatenate 'string "x" "2"))) => X2
I think this is what Barry Margolin means. If the original symbol (i.e.
either X or |x|) is read in a given case, SYMBOL-NAME will preserve
that case.
* (intern (format nil "~a2" (symbol-name 'x)))
X2
NIL
* (intern (format nil "~a2" (symbol-name '|x|)))
|x2|
NIL
*
This way you don't muck with the case of the symbol - you just append
the 2.
(Note: Here I've used (format nil ...) instead of (concatenate 'string
...) because I think the code is clearer. But you just as well could
have done (concatenate 'string (symbol-name '|x|) "2").)
In article <························@g14g2000cwa.googlegroups.com>,
"Stormbringer" <·······@mail.dntis.ro> wrote:
> Barry Margolin wrote:
> > In article <························@z14g2000cwz.googlegroups.com>,
> > "Stormbringer" <·······@mail.dntis.ro> wrote:
> >
> > > Here is my concoction:
> > >
> > > (defun new-sym(x)
> > > (intern (string-upcase (concatenate 'string (symbol-name x)
> "2"))))
> >
> > Why do you need the STRING-UPCASE? If the original symbol went
> through
> > the normal reading process, it will already have been upcased. And
> if
> > the original symbol was entered with something like |x|, they
> obviously
> > *wanted* it to be lowercase, so why should the new one be different?
>
> This is what I thought too but in practice here is what I get under
> Allegro CL Lite 5.0.1 :
>
> > (intern (concatenate 'string "x" "2")) => |x2|
> > (intern (string-upcase (concatenate 'string "x" "2"))) => X2
That's because you provided the string yourself, you didn't get it from
SYMBOL-NAME of a symbol that was originally typed in.
Take out the string-upcase in your new-sym function, and then try:
(new-sym 'x)
--
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
From: Thomas A. Russ
Subject: Re: generating a new symbol from existing one
Date:
Message-ID: <ymihdisyb5c.fsf@sevak.isi.edu>
"Stormbringer" <·······@mail.dntis.ro> writes:
> Seems to work as I wanted and I realized in the process that what I
> really want is (make-symbol <x as string>), i.e. a symbol name
> different from x.
Does GENTEMP do what you want?
--
Thomas A. Russ, USC/Information Sciences Institute
From: Thomas A. Russ
Subject: Re: generating a new symbol from existing one
Date:
Message-ID: <ymifyycyb4m.fsf@sevak.isi.edu>
"Stormbringer" <·······@mail.dntis.ro> writes:
> Seems to work as I wanted and I realized in the process that what I
> really want is (make-symbol <x as string>), i.e. a symbol name
> different from x.
Does GENTEMP do what you want?
Note that you can pass a symbol as well as a string as its first argument.
--
Thomas A. Russ, USC/Information Sciences Institute
···@sevak.isi.edu (Thomas A. Russ) writes:
> "Stormbringer" <·······@mail.dntis.ro> writes:
>
> > Seems to work as I wanted and I realized in the process that what I
> > really want is (make-symbol <x as string>), i.e. a symbol name
> > different from x.
>
> Does GENTEMP do what you want?
>
> Note that you can pass a symbol as well as a string as its first argument.
Not in Common-Lisp:
gentemp &optional prefix package => new-symbol
Arguments and Values:
prefix---a string. The default is "T".
(list (gentemp (string 'a)) (gentemp (string 'a))) --> (A2 A3)
--
__Pascal Bourguignon__ http://www.informatimago.com/
Nobody can fix the economy. Nobody can be trusted with their finger
on the button. Nobody's perfect. VOTE FOR NOBODY.
"Stormbringer" <·······@mail.dntis.ro> writes:
> Greetings,
>
> I was wondering : is it possible to write a macro that changes a symbol
> x to something like 'x2', for example :
>
> (change 'a)
> a2
> >>
>
> ... any hints ? this is not for any homeworks, just for my personal
> curiosity ... I'm sure there is some function that I should call, just
> don't know which
Why do you want a macro here? A function to do the task might look
like this:
(defun change (sym)
(let ((package (symbol-package sym))
(new-name (concatenate 'string x "2")))
(if package
(intern new-name package)
(make-symbol new-name))))
However, this will not touch the original symbol;
if you're looking for a way to change the name of a symbol,
there is no such way.
--
S. Y. A(R). A.
"Stormbringer" <·······@mail.dntis.ro> writes:
> Greetings,
>
> I was wondering : is it possible to write a macro that changes a symbol
> x to something like 'x2', for example :
>
> (change 'a)
> a2
> >>
>
> ... any hints ? this is not for any homeworks, just for my personal
> curiosity ... I'm sure there is some function that I should call, just
> don't know which
(gensym (string 'a)) --> #:A657
The real question is what would be the purpose?
--
__Pascal Bourguignon__ http://www.informatimago.com/
This is a signature virus. Add me to your signature and help me to live