From: ······@gmail.com
Subject: macro help => string into defparameter statement
Date: 
Message-ID: <1190067312.351986.291430@57g2000hsv.googlegroups.com>
I'd like to convert a string passed into a macro into a defparameter
statement like so:

(defmacro how-do-i-do-this (string-passed-in)
    (defparameter (fancy-shmancy-stuff string-passed-in) bound-to-
this))

Obviously this seems useless but I'm using this in a bigger better
macro.  Is this possible or should I bite the bullet and do it
manually?

Thanks!
-JW

From: Pillsy
Subject: Re: macro help => string into defparameter statement
Date: 
Message-ID: <1190070204.793443.254440@w3g2000hsg.googlegroups.com>
On Sep 17, 6:15 pm, ······@gmail.com wrote:
> I'd like to convert a string passed into a macro into a defparameter
> statement like so:

> (defmacro how-do-i-do-this (string-passed-in)
>     (defparameter (fancy-shmancy-stuff string-passed-in) bound-to-
> this))

> Obviously this seems useless but I'm using this in a bigger better
> macro.  Is this possible or should I bite the bullet and do it
> manually?

Without knowing the string in question, and what you want to do with
it, it's hard to give you specific advice.  In any event, you have
full access to the Common Lisp language in your macro: macros are,
after all, just functions that generate the code you want.  So you can
do whatever you want to that string in order to mangle it however you
want;  you can pick out characters using CHAR, or READ-FROM-STRING, or
even do more fancy things with your favorite regexp library.
DEFSTRUCT must be doing something like that behind the scenes.

That being said, you probably are doing something awkward and
unnecessary if you're using anything more complicated than, say,
CONCATENATE. Also, you'll almost certainly want to use INTERN to
actually make the symbol that names parameter from whatever you mangle
the string into.

Cheers,
Pillsy
From: ······@gmail.com
Subject: Re: macro help => string into defparameter statement
Date: 
Message-ID: <1190073721.160855.256400@d55g2000hsg.googlegroups.com>
intern sounds perfect!
Thanks all!
JW
On Sep 17, 4:03 pm, Pillsy <·········@gmail.com> wrote:
> On Sep 17, 6:15 pm, ······@gmail.com wrote:
>
> > I'd like to convert a string passed into a macro into a defparameter
> > statement like so:
> > (defmacro how-do-i-do-this (string-passed-in)
> >     (defparameter (fancy-shmancy-stuff string-passed-in) bound-to-
> > this))
> > Obviously this seems useless but I'm using this in a bigger better
> > macro.  Is this possible or should I bite the bullet and do it
> > manually?
>
> Without knowing the string in question, and what you want to do with
> it, it's hard to give you specific advice.  In any event, you have
> full access to the Common Lisp language in your macro: macros are,
> after all, just functions that generate the code you want.  So you can
> do whatever you want to that string in order to mangle it however you
> want;  you can pick out characters using CHAR, or READ-FROM-STRING, or
> even do more fancy things with your favorite regexp library.
> DEFSTRUCT must be doing something like that behind the scenes.
>
> That being said, you probably are doing something awkward and
> unnecessary if you're using anything more complicated than, say,
> CONCATENATE. Also, you'll almost certainly want to use INTERN to
> actually make the symbol that names parameter from whatever you mangle
> the string into.
>
> Cheers,
> Pillsy
From: ······@gmail.com
Subject: Re: macro help => string into defparameter statement
Date: 
Message-ID: <1190074599.319378.108520@y42g2000hsy.googlegroups.com>
Is there any way to get rid of the | symbols surrounding the given
word when using intern?
-JW

On Sep 17, 4:03 pm, Pillsy <·········@gmail.com> wrote:
> On Sep 17, 6:15 pm, ······@gmail.com wrote:
>
> > I'd like to convert a string passed into a macro into a defparameter
> > statement like so:
> > (defmacro how-do-i-do-this (string-passed-in)
> >     (defparameter (fancy-shmancy-stuff string-passed-in) bound-to-
> > this))
> > Obviously this seems useless but I'm using this in a bigger better
> > macro.  Is this possible or should I bite the bullet and do it
> > manually?
>
> Without knowing the string in question, and what you want to do with
> it, it's hard to give you specific advice.  In any event, you have
> full access to the Common Lisp language in your macro: macros are,
> after all, just functions that generate the code you want.  So you can
> do whatever you want to that string in order to mangle it however you
> want;  you can pick out characters using CHAR, or READ-FROM-STRING, or
> even do more fancy things with your favorite regexp library.
> DEFSTRUCT must be doing something like that behind the scenes.
>
> That being said, you probably are doing something awkward and
> unnecessary if you're using anything more complicated than, say,
> CONCATENATE. Also, you'll almost certainly want to use INTERN to
> actually make the symbol that names parameter from whatever you mangle
> the string into.
>
> Cheers,
> Pillsy
From: Pillsy
Subject: Re: macro help => string into defparameter statement
Date: 
Message-ID: <1190076377.355210.206550@k79g2000hse.googlegroups.com>
On Sep 17, 8:16 pm, ······@gmail.com wrote:

> Is there any way to get rid of the | symbols surrounding the given
> word when using intern?

Make sure that the string you're using doesn't have any "tricky"
characters like #\( or #\Space, and that it's all upper-case (provided
you haven't done anything funny with READTABLE-CASE).

Cheers,
Pillsy
From: Thomas A. Russ
Subject: Re: macro help => string into defparameter statement
Date: 
Message-ID: <ymify1b9af5.fsf@blackcat.isi.edu>
······@gmail.com writes:

> Is there any way to get rid of the | symbols surrounding the given
> word when using intern?

There are two ways to do that, basically the same sort of approach:

1)  Don't create symbol names that need the vertical bars to be
    readable.
2)  Set *readtable-case* to reduce the number of symbol names that need
    vertical bars.

The simplest way to do this is to upcase your strings before calling INTERN.

What is important is for you to understand the interaction between the
reader, *readtable-case*, interning and the printer.  The reason you are
getting vertical bars is because you have interned a symbol that
requires printing using them in order to be read in again as the same
symbol.

What you are likely not realizing is that Common Lisp is a
case-sensitive language, but one which (in its default settings) upcases
all non-escaped alphabetic characters that are read in as parts of
symbol names.  What that means is that

   (eq 'foo 'FOO)  ==>  T
   (eq 'foo 'FoO)  ==>  T

It also means that 

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

But this behavior is handled by the reader, before INTERN is given its
string argument.  INTERN does not do any case conversion, so

   (eq (intern "FOO") (intern "FOO"))  ==> T
   (eq (intern "foo") (intern "FOO"))  ==> NIL

and futhermore 

   (symbol-name (intern "FOO"))  ==>  "FOO"
   (symbol-name (intern "foo"))  ==>  "foo"
   (symbol-name '|fOo|)          ==>  "fOo"


-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Thomas A. Russ
Subject: Re: macro help => string into defparameter statement
Date: 
Message-ID: <ymik5qo96on.fsf@blackcat.isi.edu>
······@gmail.com writes:

> I'd like to convert a string passed into a macro into a defparameter
> statement like so:
> 
> (defmacro how-do-i-do-this (string-passed-in)
>     (defparameter (fancy-shmancy-stuff string-passed-in) bound-to-
> this))

You also almost certainly want to have this produce the DEFPARAMETER
form and not try to execute it at macro-expansion time.  Presumably the
simplest way to do that would be to use backquote:

(defmacro starify (name)
   `(defparameter ,(if (string= (char (string name) 0) #\*)
                       (intern (concatenate 'string "*" (string name) "*"))
                       name)
                  'bound-to-this))

   

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: D Herring
Subject: Re: macro help => string into defparameter statement
Date: 
Message-ID: <GbydnR1SmeanpnLbnZ2dnUVZ_ue3nZ2d@comcast.com>
Thomas A. Russ wrote:
> ······@gmail.com writes:
> 
>> I'd like to convert a string passed into a macro into a defparameter
>> statement like so:
>>
>> (defmacro how-do-i-do-this (string-passed-in)
>>     (defparameter (fancy-shmancy-stuff string-passed-in) bound-to-
>> this))
> 
> You also almost certainly want to have this produce the DEFPARAMETER
> form and not try to execute it at macro-expansion time.  Presumably the
> simplest way to do that would be to use backquote:
> 
> (defmacro starify (name)
>    `(defparameter ,(if (string= (char (string name) 0) #\*)
>                        (intern (concatenate 'string "*" (string name) "*"))
>                        name)
>                   'bound-to-this))

Tweaks:
(defmacro starify (name binding)
    `(defparameter ,(if (string= (char (string name) 0) #\*)
                        name
                        (intern (format nil "*~A*" name)))
                   ,binding))
(starify test 4)

- Daniel
From: Alan Crowe
Subject: Re: macro help => string into defparameter statement
Date: 
Message-ID: <86tzpsud5f.fsf@cawtech.freeserve.co.uk>
······@gmail.com writes:

> I'd like to convert a string passed into a macro into a defparameter
> statement like so:
> 
> (defmacro how-do-i-do-this (string-passed-in)
>     (defparameter (fancy-shmancy-stuff string-passed-in) bound-to-
> this))
> 
> Obviously this seems useless but I'm using this in a bigger better
> macro.  Is this possible or should I bite the bullet and do it
> manually?

I'm guessing that you are getting stuck because defparameter
requires a symbol not a string. CL:INTERN will create a
symbol, named by a string that you chose, and will register
that symbol with a package so that you can use the string to
refer to the symbol in future via the appropriate package.

CL-USER> (defmacro defgol (string)
           (let* ((separator (position #\= string))
                  (name (subseq string 0 separator))
                  (value (subseq string (+ separator 1))))
             `(defparameter ,(intern name) ,(parse-integer value))))
DEFGOL
CL-USER> (macroexpand-1 '(defgol "foo=561"))
(DEFPARAMETER |foo| 561)
T
CL-USER> (macroexpand-1 '(defgol "BAR=561"))
(DEFPARAMETER BAR 561)
T
CL-USER> (defgol "stuff =57")
|stuff |
CL-USER> |stuff |
57

There is lots to go wrong with case and white space. Good luck.

Alan Crowe
Edinburgh
Scotland