Subject: Re: concatenate atoms into string
From: (Rob Warnock)
Date: Mon, 29 Sep 2003 15:07:25 -0500
Newsgroups: comp.lang.lisp
Message-ID: <>
Brian Downing  <> 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 Warnock, PP-ASEL-IA		<>
627 26th Avenue			<URL:>
San Mateo, CA 94403		(650)572-2607