Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 218
user2040585 Avatar asked Sep 15 '25 06:09

user2040585


1 Answers

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)
like image 88
legoscia Avatar answered Sep 18 '25 11:09

legoscia