Lisp, a question about concatenation, step #4

31/07/19, modified 31/07/19

For some reason a simple thing turns out to be hard1. This is what I want to do; I want to concatenate a list of strings into a single string. I do not want this;

(concatenate 'string "no" "not" "a fixed" "list," "fuck" "it")

But this;

(somefunction alist)

Where alist is;

(list "yes" "just" "a" "list")

The answers I found so far are;

  1. Many many examples of concatenate, which does not fit in this case.
  2. Using format. I reject this because this is a different sub-language. Something like this should be doable in lisp, not a special format language.
  3. Complex functions using loop or doiterate. Really?
The one I like best so far is;
(apply #'concatenate 'string list)

But is this really the way to go? or should I make something based on reduce?

Like this;

(reduce (lambda (a b) (concatenate 'string a b)) (list "why" "no" "sharp-quote" "for" "the"  "lambda?"))
  1. I must not be thinking very lispy yet or something []

5 Responses to “Lisp, a question about concatenation, step #4”

  1. spyked says:

    Unfortunately (apply #'concatenate 'string list) has the problem that on *some* implementations it might hit the call-arguments-limit wall, i.e. the total number of parameters (including the length of the list) shouldn't be higher than that.

    See discussion for example.

    One problem with concatenate itself would be that it does a lot of copying if applied repeatedly (in conjunction with reduce). A more conservative (yet less legible) approach would be to pre-allocate a string with the size being that of the sum of all constituent strings, then repeatedly applying "replace" to fill in the target string. For example CL-WHO has string-list-to-string which works exactly like this.

  2. Re: 2: "format" and "loop" are made of lisp macros. Their text is reproduced in CLTL2.

    The idea was, if they did not exist, will end up written regardless, so may as well standardize. CL programmer is not forced to use them in any sense, but you will encounter them in published programs, so must at least know how to read.

  3. The problem is your notion of strings.

  4. ave1 says:


    Thank you, yes but I wanted to avoid "format" for now (as I can think of the same use cases of lists with other items). And loop will probably be next on the menu, but I hoped there would be other methods.

    I'm probably still thinking too much in Python idoms (with *join* of strings)


    I do not understand, is this something that can be explained? I can also image I'll have to continue on this path for a long while to get an inkling.

  5. Your notion of strings is not even C, seems rather php/python/perl etc, where "hurr"."durr"="hurrdurr" as a "just like that" thing.

    That dot is not merely an undefined operation in lisp, it is an undefinable operation, and for fundamental reasons, having to do with the very definitions of terms. You can start the trip this way : write down your own definition of "string", see what lisp primitive it matches ; maybe notice in the process you didn't even think before a definition is needed, "shit should just work", and all that.

    Why do you want strings ? What do you use them for ? Why are you using strings for that ? And so following.

Leave a Reply