How can I achieve below in Scheme REPL? Create a variable name from a string.
=>(define (string->variable-name "foo") 12)
=>foo
12
=>(+ foo 8)
20
In Common Lisp, this should be
=> (set (intern "ANY-TEXT") 5)
=> ANY-TEXT
5
How do I build a #procedure like "string->variable-name" (and "variable-name->string") ?
Thanks a lot.
Forgetting the syntax for a moment:
Sure you can't do
(+ 5 <my-named-value>)
without some other help...If what you're passing to
string->variable-name
is always a string literal (i.e., not a variable that contains a string), you can do that using asyntax-case
macro that transforms the string literal to an identifier:and conversely, a
variable-name->string
macro could look like this:However, remember: this will only work if you are working with string (in case of
string->variable-name
) or identifier (in case ofvariable-name->string
) literals.If, on the other hand, you want the ability to reflect on names in your current Scheme environment, this is not supported by standard Scheme. Some implementations, like Guile or Racket, do have this capability.
Here's a Guile example:
and a Racket example: