1

I need to define a variable with a name contained in another variable.

The following code does not work and it uses eval, therefore not very good style.

(defvar symbol "zap")
(eval `(defvar ,symbol 100))
(print zap)

4 Answers 4

1

It's usually better to use a hash table for that purpose.

1

First of all -- are you sure you need to do this?

Now, with that out of the way --

(defvar *symbol-name* "zap")
(let ((symbol (intern *symbol-name*)))
  (proclaim `(special ,symbol))
  (setf (symbol-value symbol) 100))
(print |zap|)

Note that |zap| will normally need to be quoted with pipes, because intern is case-sensitive, while the default readtable upcases symbols by default. So if you don't quote, as in (print zap), that is interpreted as (PRINT ZAP), with the wrong case for zap. Alternatively, you can upcase the interned symbol, with (intern (string-upcase *symbol-name*)).


Update after seeing what you're actually trying to do.

You definitely don't want to be defining global variables for that. A data structure that maps keys (A1...H4, etc) to numbers is what you want. In this case, your keys can be symbols or strings. And the data structure could be a hashtable, a plist or alist (among other choices). For example, a string-keyed hashtable would look like:

(defvar *table* (make-hash-table :test #'equalp))
(setf (gethash "A1" *table*) 42)
(print (gethash "A1" *table*)) ==>  prints 42
2
  • Thank you very much, Daniel. What I am trying to achive is to have 64 Variables with names "A1".."H8", that contain the values 21..98. A1=21,...,H1=28, A2=31,..,H2=38,....H8=98. So I started with (dolist (c '(a b c d e f g h i j)) (dolist (r '(1 2 3 4 5 6 7 8)) (defvar ...))). Is there a better aproach for this task?
    – Patrick
    Nov 8, 2010 at 16:23
  • Yeah you definitely don't want to be defining a bunch of global variables for that (imagine what would happen if you actually use a variable called H1 at some point in the program). Check out the updated answer. Nov 8, 2010 at 17:07
0

If you absolutely want to do this, with global variables, I suspect that SET and SYMBOL-VALUE (and using symbols instead of strings) might do the trick. It definitely falls in the "hm, yes, you can do it that way, but I am not convinced it's the best way" territory, though.

However, you are most probably better off either using a hash-table or (skipping storage completely, if there's no need to mutate what A1 means further down the line) a function that parses the letter and digit apart and calculates the relevant value.

1
  • the reason why I considered this to be usefull is: The A1..H8 are inices into an array. This array has to work with indices 0..119 for reasons that arise from the application (chessboard). I thought it to be convenient to be able to say (setf (aref board a1) 'king) and so on. But I also see, that the global namespace might not be the right place for the field names. Thank you.
    – Patrick
    Nov 9, 2010 at 16:16
0

use SET:

CL-USER 13 > (defvar *some-symbol* '*this-is-some-symbol*)
*SOME-SYMBOL*

CL-USER 14 > (set *some-symbol* 42)
42

CL-USER 15 > *this-is-some-symbol*
42

or if it is a string:

(setf (symbol-value (find-symbol some-symbol-name)) 42)
1
  • how could i forget that there is non only a "quoted set" (setq) but also the generic "set" instruction. I suspect that I am thinking little to complicated, sometimes. Thank you, Rainer!
    – Patrick
    Nov 9, 2010 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.