1

I want to string-join a list of strings, one of which is returned by a function call.

Like this:

(defun foobar () "foobar")
(string-join '("foo" (foobar) "bar") "|")

That results in Wrong type argument: characterp, foobar and not in "foo|foobar|bar" as expected.

What's the elegant way to do that?

Drew
  • 75,699
  • 9
  • 109
  • 225
  • https://emacs.stackexchange.com/tags/elisp/info – Drew Jan 17 '21 at 00:49
  • 1
    Does this answer your question? [How to evaluate the variables before adding them to a list?](https://emacs.stackexchange.com/questions/7481/how-to-evaluate-the-variables-before-adding-them-to-a-list) – Drew Jan 17 '21 at 19:04
  • There are multiple questions that deal with the same thing: quoting some sexp and expecting some part of the quoted sexp to be evaluate. It would be great if Someone (TM) created a general Q & A for that, as a Community question. Many, many questions like this one have been closed as dups of the one I cited, but that Q is not stated in as general a way as it could be. – Drew Jan 17 '21 at 19:06

1 Answers1

2

A quoted list doesn't evaluate its args, so it consists of a string, a list containing a symbol and another string. You can selectively evaluate it using backquote and unquote:

`("foo" ,(foobar) "bar") ;=> ("foo" "foobar" "bar")
(string-join `("foo" ,(foobar) "bar") "|") ;=> "foo|foobar|bar"
Muihlinn
  • 2,576
  • 1
  • 14
  • 22
wasamasa
  • 21,803
  • 1
  • 65
  • 97