Skip to content

Instantly share code, notes, and snippets.

@lispm
Last active December 11, 2016 22:02
Show Gist options
  • Save lispm/754585a9be5451ec900cb280670867f6 to your computer and use it in GitHub Desktop.
Save lispm/754585a9be5451ec900cb280670867f6 to your computer and use it in GitHub Desktop.
; a join function using FORMAT
(defun join-aux (stream &rest args)
(declare (ignore args)
(special delim))
(princ delim stream))
(defun join (list delim)
(declare (special delim))
(format nil "~{~a~^~/JOIN-AUX/~:*~}" list))
; I haven't seen a possibility to pass the delimiter directly, so that
; some format directive can make use of it. We have to work around it.
;
; The trick:
; * a global function JOIN-AUX takes a stream and ignores the rest of its
; args and prints the delimiter instead
; * the delimiter is accessed via a special variable using dynamic binding
; * in the format string, the function JOIN-AUX gets called with the next argument
; and then we back off using ~:* this one argument, so that it remains unprocessed
; for the iteration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment