28

I want to convert ("USERID=XYZ" "USERPWD=123") to "USERID=XYZ&USERPWD=123". I tried

(apply #'concatenate 'string '("USERID=XYZ" "USERPWD=123"))

which will return ""USERID=XYZUSERPWD=123".

But i do not know how to insert '&'? The following function works but seems a bit complicated.

(defun join (list &optional (delim "&"))
    (with-output-to-string (s)
        (when list
            (format s "~A" (first list))
            (dolist (element (rest list))
               (format s "~A~A" delim element)))))
N.N.
  • 8,336
  • 12
  • 54
  • 94
z_axis
  • 8,272
  • 7
  • 41
  • 61

6 Answers6

52

Use FORMAT.

~{ and ~} denote iteration, ~A denotes aesthetic printing, and ~^ (aka Tilde Circumflex in the docs) denotes printing the , only when something follows it.

* (format nil "~{~A~^, ~}" '( 1 2 3 4 ))

"1, 2, 3, 4"
* 
Paul Nathan
  • 39,638
  • 28
  • 112
  • 212
  • This unfortunately does not work in Emacs "elisp". They have a different format function. Is there any comparable way to do this in Emacs? – killdash9 Oct 25 '13 at 23:18
  • 1
    @russ: Probably. Not being an elisp wizard, I went back to basic Lisp... `(defun join-to-str (thing &rest strings) (labels ((recurser (strings) (cond ((> (length strings) 1) (append (list (car strings) thing) (recurser (cdr strings)))) (t (cons (car strings) nil))))) (apply 'concat (recurser strings))))` – Paul Nathan Oct 26 '13 at 20:47
  • 4
    @russ better late than never, if you're concatenating strings: `(mapconcat 'identity '("one" "two") ", ")` but if they aren't strings, you want to change `'identity` to `(lambda (x) (format "%s" x))` – spacebat Oct 06 '15 at 06:27
8

This solution allows us to use FORMAT to produce a string and to have a variable delimiter. The aim is not to cons a new format string for each call of this function. A good Common Lisp compiler also may want to compile a given fixed format string - which is defeated when the format string is constructed at runtime. See the macro formatter.

(defun %d (stream &rest args)
  "internal function, writing the dynamic value of the variable
DELIM to the output STREAM. To be called from inside JOIN."
  (declare (ignore args)
           (special delim))
  (princ delim stream))

(defun join (list delim)
  "creates a string, with the elements of list printed and each
element separated by DELIM"
  (declare (special delim))
  (format nil "~{~a~^~/%d/~:*~}" list))

Explanation:

"~{      iteration start
 ~a      print element
 ~^      exit iteration if no more elements
 ~/%d/   call function %d with one element
 ~:*     move one element backwards
 ~}"     end of iteration command

%d is just an 'internal' function, which should not be called outside join. As a marker for that, it has the % prefix.

~/foo/ is a way to call a function foo from a format string.

The variables delim are declared special, so that there can be a value for the delimiter transferred into the %d function. Since we can't make Lisp call the %d function from FORMAT with a delimiter argument, we need to get it from somewhere else - here from a dynamic binding introduced by the join function.

The only purpose of the function %d is to write a delimiter - it ignores the arguments passed by format - it only uses the stream argument.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • Dear Rainer, is there a reason, why you make the variable name begin with `%`? Is the `(declare (special delim))` kind of a way to make the `delim` variable bind dynamically or so? How does this function exactly work? Why the `%d` function needs those arguments? – Gwang-Jin Kim Jun 01 '18 at 17:27
  • Where one can find anythng about the `~/ /` directive? - I couldn't find about it anything in CLTL ... – Gwang-Jin Kim Jun 01 '18 at 17:37
  • @coredump Thank you! Okay, still learning where to search/gather the information for cl stuff - obviously :D. – Gwang-Jin Kim Jun 01 '18 at 19:04
  • 1
    @Gwang-JinKim: you can find `~/` in the Common Lisp Hyperspec, use the master index, non-alphabetic chars: http://www.lispworks.com/documentation/HyperSpec/Front/X_Mast_9.htm – Rainer Joswig Jun 01 '18 at 19:16
  • @RainerJoswig: Thank you very much! Thank you for the detailed explanation! This is sth I was searching. However, do you have an idea about my `"#\\Tab"` as `"#\Tab"` problem in my question? – Gwang-Jin Kim Jun 01 '18 at 20:03
  • @Gwang-JinKim: which tab problem? Make a separate question. Note that my function does: `(join '(1 2 3 4 5) #\tab)` -> `"1 2 3 4 5"` - with tab characters between the numbers. – Rainer Joswig Jun 01 '18 at 20:07
  • @Rainer - just right now I see that someone asked exactly the question I want to post (or did post). My question: https://stackoverflow.com/questions/50647635/nicer-pythonic-join-in-common-lisp?noredirect=1#comment88310300_50647635 and other persons: https://stackoverflow.com/questions/23577195/common-lisp-printing-the-tab-character-in-function-format – Gwang-Jin Kim Jun 01 '18 at 20:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172272/discussion-between-gwang-jin-kim-and-rainer-joswig). – Gwang-Jin Kim Jun 01 '18 at 20:23
6

Melpa hosts the package s ("The long lost Emacs string manipulation library"), which provides many simple string utilities, including a string joiner:

(require 's)                                                                                                                                                                                          
(s-join ", " (list "a" "b" "c"))                                                                                                                                                                      
"a, b, c"

For what it's worth, very thinly under the hood, it's using a couple lisp functions from fns.c, namely mapconcat and identity:

(mapconcat 'identity (list "a" "b" "c") ", ")                                                                                                                                                         
"a, b, c"
preskitt91
  • 61
  • 1
  • 2
3

A bit late to the party, but reduce works fine:

(reduce (lambda (acc x)
          (if (zerop (length acc))
              x
              (concatenate 'string acc "&" x)))
        (list "name=slappy" "friends=none" "eats=dogpoo")
        :initial-value "")
andrew
  • 2,819
  • 24
  • 33
2

Assuming a list of strings and a single character delimiter, the following should work efficiently for frequent invocation on short lists:

(defun join (list &optional (delimiter #\&))
  (with-output-to-string (stream)
    (join-to-stream stream list delimiter)))

(defun join-to-stream (stream list &optional (delimiter #\&))
  (destructuring-bind (&optional first &rest rest) list
    (when first
      (write-string first stream)
      (when rest
        (write-char delimiter stream)
        (join-to-stream stream rest delimiter)))))
nowbumja
  • 53
  • 3
1

With the newish and simple str library:

(ql:quickload "str")
(str:join "&" '("USERID=XYZ" "USERPWD=123"))

It uses format like explained in the other answers:

(defun join (separator strings)
" "
(let ((separator (replace-all "~" "~~" separator)))
  (format nil
        (concatenate 'string "~{~a~^" separator "~}")
        strings)))

(author of it, to make simple things like this simple).

Ehvince
  • 17,274
  • 7
  • 58
  • 79