* A variable that holds a string which may be the name of a variable.
@ 2009-12-02 19:22 Richard Shann
2009-12-02 22:19 ` Neil Jerram
2009-12-02 22:37 ` Linas Vepstas
0 siblings, 2 replies; 8+ messages in thread
From: Richard Shann @ 2009-12-02 19:22 UTC (permalink / raw)
To: guile-user
I am stuck on one of those symbol/variable-name-in-a-string things
again:
(define mything "display")
(display (eval-string mything))
that's fine. But can I test that the string in mything is the name of a
variable before doing the eval-string and finding out the hard way? I've
been doing (symbol? mything) etc, and going witless. Do I have to do all
that catch stuff?
The situation is that I can guess (programmatically construct that is)
the name of the variable, but it may not be defined. If it isn't I will
just say so, but if it is I want the string it is defined to hold.
Any help much appreciated!
Richard Shann
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: A variable that holds a string which may be the name of a variable.
2009-12-02 19:22 A variable that holds a string which may be the name of a variable Richard Shann
@ 2009-12-02 22:19 ` Neil Jerram
2009-12-03 17:23 ` Linas Vepstas
2009-12-02 22:37 ` Linas Vepstas
1 sibling, 1 reply; 8+ messages in thread
From: Neil Jerram @ 2009-12-02 22:19 UTC (permalink / raw)
To: richard.shann; +Cc: guile-user
Richard Shann <richard.shann@virgin.net> writes:
> I am stuck on one of those symbol/variable-name-in-a-string things
> again:
>
> (define mything "display")
> (display (eval-string mything))
>
> that's fine. But can I test that the string in mything is the name of a
> variable before doing the eval-string and finding out the hard way? I've
> been doing (symbol? mything) etc, and going witless. Do I have to do all
> that catch stuff?
>
> The situation is that I can guess (programmatically construct that is)
> the name of the variable, but it may not be defined. If it isn't I will
> just say so, but if it is I want the string it is defined to hold.
>
> Any help much appreciated!
Hi Richard... :-)
You can use defined? to find out if an arbitrary symbol is defined (in
the current module).
It takes a symbol, so for example:
(let ((sym (with-input-from-string mything read)))
(if (defined? sym)
(eval sym (current-module))
(display "Sorry, that isn't defined")))
Does that help?
Regards,
Neil
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: A variable that holds a string which may be the name of a variable.
2009-12-02 19:22 A variable that holds a string which may be the name of a variable Richard Shann
2009-12-02 22:19 ` Neil Jerram
@ 2009-12-02 22:37 ` Linas Vepstas
1 sibling, 0 replies; 8+ messages in thread
From: Linas Vepstas @ 2009-12-02 22:37 UTC (permalink / raw)
To: richard.shann; +Cc: guile-user
2009/12/2 Richard Shann <richard.shann@virgin.net>:
> I am stuck on one of those symbol/variable-name-in-a-string things
> again:
>
> (define mything "display")
> (display (eval-string mything))
>
> that's fine. But can I test that the string in mything is the name of a
> variable before doing the eval-string and finding out the hard way? I've
> been doing (symbol? mything) etc, and going witless. Do I have to do all
> that catch stuff?
If I understand you correctly, no. That's because the
thing going into eval-string can be arbitrarily complex --
e.g. (eval-string "(define foo (lambda (x) (+ x y))) (define (bar
z y) (foo (z)) (bar 2 2)") and there's no particular way to know
if evaluating that won't throw some error. -- really, you have to
evaluate it to find out.
If you want to find out if some particular variable is bound
to a proceedure, you can try (proceedure? thing) see e.g.
http://www.gnu.org/software/guile/manual/html_node/Procedure-Properties.html
If you want to know if a variable is bound, try defined? e.g.
guile> (defined? 'x)
#f
guile> (defined? (string->symbol "x"))
#f
guile> (define x 42)
guile> (defined? 'x)
#t
guile> (defined? (string->symbol "x"))
#t
--linas
--linas
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: A variable that holds a string which may be the name of a variable.
@ 2009-12-03 16:11 Richard Shann
2009-12-03 18:52 ` Neil Jerram
0 siblings, 1 reply; 8+ messages in thread
From: Richard Shann @ 2009-12-03 16:11 UTC (permalink / raw)
To: guile-user
Thanks to all guileans who have responded. I seem to have a workable
solution based on Neil's code. The thing I hadn't grokked was the
existence of the with-input-from-string procedure. I had been messing
with defined? and symbol? but got nowhere.
FWIW the core bit of code I ended up with takes the name of a command,
<name> and looks for a string in a variable called Help-d-<name> if it
exists:
(set! help (string-append "Help-d-" name))
(let ((sym (with-input-from-string help read)))
(if (defined? sym)
(set! help (eval sym (current-module)))
(set! help "No help")
))
This seems to cover all the cases. I haven't really got to the bottom of
what that with-input-from-string procedure does - I got as far as the
documentation for read which mentions an s-expression. Elsewhere I have
seen references to "forms", perhaps there is something establishing some
basic terminology that I have missed...
But thanks a lot - I am on my way again...
Richard
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: A variable that holds a string which may be the name of a variable.
2009-12-02 22:19 ` Neil Jerram
@ 2009-12-03 17:23 ` Linas Vepstas
2009-12-03 18:56 ` Neil Jerram
0 siblings, 1 reply; 8+ messages in thread
From: Linas Vepstas @ 2009-12-03 17:23 UTC (permalink / raw)
To: Neil Jerram; +Cc: guile-user, richard.shann
2009/12/2 Neil Jerram <neil@ossau.uklinux.net>:
Neil,
For my own edification -- is tehre any difference between
> (let ((sym (with-input-from-string mything read)))
and
(let ((sym (string->symbol mything)))
?
--linas
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: A variable that holds a string which may be the name of a variable.
2009-12-03 16:11 Richard Shann
@ 2009-12-03 18:52 ` Neil Jerram
0 siblings, 0 replies; 8+ messages in thread
From: Neil Jerram @ 2009-12-03 18:52 UTC (permalink / raw)
To: richard.shann; +Cc: guile-user
Richard Shann <richard.shann@virgin.net> writes:
> Thanks to all guileans who have responded. I seem to have a workable
> solution based on Neil's code. The thing I hadn't grokked was the
> existence of the with-input-from-string procedure. I had been messing
> with defined? and symbol? but got nowhere.
I would say the magic is more in `read' than in
`with-input-from-string'.
`read' is responsible for the conversion from
something-that-looks-like-code to actual code - which in your case is a
single symbol. `with-input-from-string' just tells `read' that the
something-that-looks-like-code is a string.
Regards,
Neil
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: A variable that holds a string which may be the name of a variable.
2009-12-03 17:23 ` Linas Vepstas
@ 2009-12-03 18:56 ` Neil Jerram
2009-12-03 19:00 ` Richard Shann
0 siblings, 1 reply; 8+ messages in thread
From: Neil Jerram @ 2009-12-03 18:56 UTC (permalink / raw)
To: linasvepstas; +Cc: guile-user, richard.shann
Linas Vepstas <linasvepstas@gmail.com> writes:
> 2009/12/2 Neil Jerram <neil@ossau.uklinux.net>:
> Neil,
>
> For my own edification -- is tehre any difference between
>
>> (let ((sym (with-input-from-string mything read)))
>
> and
>
> (let ((sym (string->symbol mything)))
In the case where mything is a string consisting of symbol syntax
characters, no.
Maybe you're then suggesting that Richard could use string->symbol
instead - in which case I agree. Using with-input-from-string and read
is a bit of overkill; I forgot about string->symbol.
Regards,
Neil
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: A variable that holds a string which may be the name of a variable.
2009-12-03 18:56 ` Neil Jerram
@ 2009-12-03 19:00 ` Richard Shann
0 siblings, 0 replies; 8+ messages in thread
From: Richard Shann @ 2009-12-03 19:00 UTC (permalink / raw)
To: Neil Jerram; +Cc: guile-user
On Thu, 2009-12-03 at 18:56 +0000, Neil Jerram wrote:
> Linas Vepstas <linasvepstas@gmail.com> writes:
>
> > 2009/12/2 Neil Jerram <neil@ossau.uklinux.net>:
> > Neil,
> >
> > For my own edification -- is tehre any difference between
> >
> >> (let ((sym (with-input-from-string mything read)))
> >
> > and
> >
> > (let ((sym (string->symbol mything)))
>
> In the case where mything is a string consisting of symbol syntax
> characters, no.
In fact, I was somewhat interested that it should be fairly bomb proof
to the contents of mything. So I tried Neil's code on various dangerous
looking things...
I hadn't spotted string->symbol, else I might have got there unaided; I
had a hazy idea that that was what I was trying to do. Gradually the
mists are clearing - thank you.
Richard
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2009-12-03 19:00 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-12-02 19:22 A variable that holds a string which may be the name of a variable Richard Shann
2009-12-02 22:19 ` Neil Jerram
2009-12-03 17:23 ` Linas Vepstas
2009-12-03 18:56 ` Neil Jerram
2009-12-03 19:00 ` Richard Shann
2009-12-02 22:37 ` Linas Vepstas
-- strict thread matches above, loose matches on Subject: below --
2009-12-03 16:11 Richard Shann
2009-12-03 18:52 ` Neil Jerram
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).