Emacs Asked by shackra on September 2, 2021
I have a variable with some common values between dap-mode
debug templates and I want to use that variable along with other cons elements for :environment-variable
in languages like Golang or Typescript you would do something like this:
variable = [1, 2, 3]
another_variable = [...variable, 4, 5, 6]
then another_variable
will carry the value of [1, 2, 3, 4, 5, 6]
How can I do this in Emacs Lisp?
EDIT: I know how you can evaluate something inside a list, this is using the backquote, my problem was that I didn’t know how Elisp processes lists in a fashion that keep the result plain because, for instance in Python, you can end with something like [1, [2, 3, 4]]
if x
is [1]
and you do x.append([2, 3, 4])
; I didn’t know that ,@
was a thing and that neither append
keeps a plain list.
You can use the backquote mechanism in combination with ,@
to splice the value of a variable into a list; or equivalently, use append
:
(setq variable '(1 2 3))
(1 2 3)
(setq another-variable `(-2 -1 0 ,@variable 4 5 6))
(-2 -1 0 1 2 3 4 5 6)
;; or...
(setq another-variable (append '(-2 -1 0) variable '(4 5 6)))
(-2 -1 0 1 2 3 4 5 6)
Correct answer by NickD on September 2, 2021
Note: A Lisp program should not try to modify self-evaluating forms or constant lists (bug#40671). These are internal data structures [Message 42].
Be aware that the final argument for append
is not copied.
ELISP> (setq var1 (vector 'a 'b))
ELISP> (symbol-value 'var1)
[a b]
ELISP> (setq lst (append var1 (list 1)))
ELISP> (symbol-value 'lst)
(a b 1)
Answered by user19761 on September 2, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP