1

I'm a newbie in ELisp and I can't set a variable value to access later. I read the documentation on accessing variable values and tried the answers in this thread, but I am still getting Wrong type argument: char-or-string-p.

Here is the output of my *scratch* Emacs buffer, which runs the code and inserts it after any side effects:

(defvar my-var "var"  "Some documentation")
my-var

my-var
"var"

(symbol-value 'my-var)
"var"

I can iterate on a list made from the value as a literal:

(dolist (v '("var"))
  (insert v))
varnil

But each of these attempts to iterate on the value as a variable fail with the same error Wrong type argument: char-or-string-p:

(dolist (v '(my-var))
  (insert v))

(dolist (v '((format "%S" my-var)))
  (insert v))

(dolist (v '((symbol-value 'my-var)))
  (insert v))

How can I iterate on a list made from values of variables?

miguelmorin
  • 5,025
  • 4
  • 29
  • 64
  • 1
    Create lists with the function LIST. Using a quote only gives you literal data. The arguments to LIST will be evaluated and the results will be put into a list. `(list (+ 1 2)) -> (3)` vs. `'((+ 1 2)) -> ((+ 1 2))` – Rainer Joswig Sep 26 '19 at 12:29
  • 1
    Note that your `my-var`, `(format "%S" my-var)` and `(symbol-value 'my-var)` expressions do not get evaluated, because they are inside a quote. – Kaz Sep 27 '19 at 01:41
  • This comment might also help: https://stackoverflow.com/questions/56523328/the-list-inside-a-list-of-lisp-tutorial#comment99673439_56523328 – phils Oct 09 '19 at 05:40

1 Answers1

4

You need to either evaluate your variables:

(dolist (v (list my-var))
  (insert v))

or get the values yourself:

(dolist (v (list 'my-var))
  (insert (symbol-value v)))

If you want to mix variables and literals, then use the first approach:

(defvar my-var "var"  "Some documentation")

(dolist (d (list my-var
                 "other-var"))
  (insert d))

See also When to use ' (or quote) in Lisp?

sds
  • 58,617
  • 29
  • 161
  • 278