guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A variable that holds a string which may be the name of a variable.


From: Linas Vepstas
Subject: Re: A variable that holds a string which may be the name of a variable.
Date: Wed, 2 Dec 2009 16:37:37 -0600

2009/12/2 Richard Shann <address@hidden>:
> 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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]