Search code examples
elispstring-concatenationquoting

using mapconcat to concatenate a list containing a variable


I'm trying to join a list of strings using mapconcat, but can't figure out how to include a variable as one of the list elements. Here's what I'm going for:

(mapconcat #'identity '("" "path" "to" "someplace") "/")
=> "/path/to/someplace"

But when I try to include a variable:

(let ((path "someplace"))
  (mapconcat #'identity '("" "path" "to" path) "/"))
=> Wrong type argument: sequencep, path

This doesn't work either:

(let ((path "someplace"))
  (mapconcat #'(lambda (x) (format "%s" x)) '("" "path" "to" path) "/"))
=> "/path/to/path"

Can anyone point out what I'm missing here?


Solution

  • You're quoting the list with ', which means that any symbols are included as symbols in the list, instead of being dereferenced as variables.

    You can either use the list function:

    (list "" "path" "to" path)
    

    or use a backquote and a comma, to force evaluation of one of the list elements:

    `("" "path" "to" ,path)