How to use variable names inside literals in Emacs Lisp?

Is it possible to write, for example. a vector literal that uses a variable inside, so that the variable gets the correct estimate and the resulting vector does not just contain the name / symbol of the variable?

For instance:

(setq inner ["d" "e"]) ["a" "b" inner] 

Results in:

 ["a" "b" inner] 

But I would like to:

["a" "b" ["d" "e"]]

I did some Clojure coding before Elisp, there it works as I expected:

 (def inner ["d" "e"]) user=> ["a" "b" inner] ["a" "b" ["d" "e"]] 

What is the main thing that I don’t understand about Elisp here? I can, of course, get around this, but I would like to understand what is happening.

+6
source share
2 answers

Using vector syntax, elements are considered constant. In the Emacs Lisp 6.4 Reference Guide:

A vector, like a string or a number, is considered a constant for evaluation: the result of the evaluation is the same vector. This does not evaluate or even check the elements of the vector.

Alternatively, you can use the vector function to create a vector in which elements are evaluated:

 (setq inner ["d" "e"]) (vector "a" "b" inner) => ["a" "b" ["d" "e"]] 
+12
source

Use backquote literal. It can be used in the same way as for a list.

 (setq inner ["d" "e"]) `["a" "b" ,inner] => ["a" "b" ["d" "e"]] 
+8
source

Source: https://habr.com/ru/post/906527/


All Articles