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;
- Many many examples of concatenate, which does not fit in this case.
- 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.
- 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?"))
- I must not be thinking very lispy yet or something [↩]
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.
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.
The problem is your notion of strings.
Stan:
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)
MP
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.
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.