concatenate is an alias for ‘cl-concatenate’ in ‘cl.el’.
(concatenate TYPE SEQUENCE...)
Concatenate, into a sequence of type TYPE, the argument SEQUENCEs.
So for your example
(concatenate 'list '("a" "b" "c") '("d" "e" "f"))
Since it's defined in cl
you may have to (require 'cl)
first, otherwise you can use cl-concatenate
which seems to be loaded by default.
Also, as pointed out by @phils cl-concatenate
just calls append
when TYPE is 'list
, here's the source from cl-extra.el
:
(defun cl-concatenate (type &rest sequences)
"Concatenate, into a sequence of type TYPE, the argument SEQUENCEs.
\n(fn TYPE SEQUENCE...)"
(pcase type
(`vector (apply #'vconcat sequences))
(`string (apply #'concat sequences))
(`list (apply #'append (append sequences '(nil))))
(_ (error "Not a sequence type name: %S" type))))
So if you're only using lists, it's simpler to directly use append
, as pointed out by @John Kitchin.
Finally, @lawlist mentionned nconc
:
nconc is a built-in function in ‘C source code’.
(nconc &rest LISTS)
Concatenate any number of lists by altering them.
Only the last argument is not altered, and need not be a list.
What this means:
(nconc '("a" "b" "c") '("d" "e" "f"))
=> ("a" "b" "c" "d" "e" "f")
(setq l1 '("a" "b" "c")
l2 '("d" "e" "f"))
(nconc l1 l2)
=> ("a" "b" "c" "d" "e" "f")
l1
=> ("a" "b" "c" "d" "e" "f")
l2
=> ("d" "e" "f")
append
is the correct answer here, but another (destructive) way to do it would be(setcdr (last a) b)
.(-concat '(1) '(2 3) '(4)) ;; => '(1 2 3 4)
add-to-list
in Lisp code (it says this in the docstring). Instead, usepush
.