Subject: Re: concatenate atoms into string
From: rpw3@rpw3.org (Rob Warnock)
Date: Mon, 29 Sep 2003 15:07:25 -0500
Newsgroups: comp.lang.lisp
Message-ID: <vdidnTzdUcZgDeWiXTWc-w@speakeasy.net>
Brian Downing  <see-signature@lavos.net> wrote:
+---------------
| Well, this only makes one call to concatenate, hardly conses otherwise,
| and is general for all sequences.  I like it.
| 
| (defun join-seq (type list separator)
|   (let ((concatenate-args
|          (cons (first list) (loop for element in (rest list)
|                                   collect separator
|                                   collect element))))
|     (apply #'concatenate type concatenate-args)))
+---------------

An idiom I've found myself using with LOOP to special-case
first elements is this:

  (defun join-seq (type list separator)
    (apply #'concatenate type
	   (loop for element in list
		 and first = t then nil
             unless first
	       collect separator
	     collect element)))

or, depending on the phase of the moon and what I've had for lunch,
this [note use of ON here]:

  (defun join-seq (type list separator)
    (apply #'concatenate type
	   (loop for tail on list
	     collect (car tail)
	     when (cdr tail)
	       collect separator)))

But both of these do involve a test per iteration that yours doesn't,
so are slightly less efficient I suppose.


-Rob

-----
Rob Warnock, PP-ASEL-IA		<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607