I would like to concatenate a string (e.g. "abc") and a list (e.g. '(aa bb)) so as to obtain "abc aa bb".
3 Answers
AFAIK the canonical way to turn a list into a string is with mapconcat, e.g.
(defun concat-string-list (str xs)
(concat str " "
(mapconcat #'symbol-name xs " ")))
-
Why
#? is not it a symbol?Francesco Cadei– Francesco Cadei2018-10-16 22:34:26 +00:00Commented Oct 16, 2018 at 22:34 -
1
M-x elisp-index-search RET #' RETphils– phils2018-10-16 23:00:04 +00:00Commented Oct 16, 2018 at 23:00 -
why do I get
mapconcat: Wrong type argument: symbolp, "my string"but works witheval? it's lexical binding?Muihlinn– Muihlinn2020-05-06 15:59:32 +00:00Commented May 6, 2020 at 15:59 -
@Muihlinn: No. [ Given the lack of details you give, I can't give much more details either. ]Stefan– Stefan2020-05-06 17:36:36 +00:00Commented May 6, 2020 at 17:36
-
oh, just realized that it's a list of symbols, not a list of strings as I thought first and couldn't see beyond that for a while..Muihlinn– Muihlinn2020-05-07 07:02:25 +00:00Commented May 7, 2020 at 7:02
Does the content of the list always consist of symbols like that? If you just want the string representation of the content of the list (i.e. the printed list without the parens) you could do something like:
(let* ((my/str "abc ")
(my/list '(aa bb)))
(concat
my/str
(substring (format "%s" my/list) 1 -1)))
;; "abc aa bb"
This is another method that assumes the list always contains symbols. It uses #'symbol-name to convert each symbol to a string.
ELISP> (defun string-and-list-separated-by-space (string lst)
(string-join (cons string
(mapcar #'symbol-name
lst))
" "))
string-and-list-separated-by-space
ELISP> (string-and-list-separated-by-space "abc" '(aa bb))
"abc aa bb"
(let ((some_list '(aa bb))) (cons "abc " some_list))but it returns("abc " aa bb)aaandbb? symbols will be'aaand'bb. strings will be"aa"and"bb". are they vars?