I'm trying to take a string, and convert it into a variable name. I though (make-symbol) or (intern) would do this, but apparently it's not quite what I want, or I'm using it incorrectly.
For example:
> (setf (intern (string "foo")) 5)
> foo
5
Here I would be trying to create a variable named 'foo' with a value of 5. Except, the above code gives me an error. What is the command I'm looking for?
Use:
This also works with a variable:
Remember also that by default symbols are created internally as uppercase. If you want to access a symbol via a string, you have to use an uppercase string then.
There are a number of things to consider here:
SETF
does not evaluate its first argument. It expects a symbol or a form that specifies a location to update. UseSET
instead.Depending upon the vintage and settings of your Common Lisp implementation, symbol names may default to upper case. Thus, the second reference to
foo
may actually refer to a symbol whose name is"FOO"
. In this case, you would need to use(intern "FOO")
.The call to
STRING
is harmless but unnecessary if the value is already a string.Putting it all together, try this: