From: ·······@gmail.com
Subject: format question
Date: 
Message-ID: <1154635106.641254.159050@b28g2000cwb.googlegroups.com>
Hi, I am a newbie and I'm learning about format directives. I'm
creating a function that prints a list with a particular delimiter
(character). The arguments to the function are theList and Delimiter. I
can think of a solution using loop, but I was wondering if it was
possible to also use only the format directives. Here's my code (works
except it throws one last delimiter at the end):

(defun ListPrint (Delimiter theList)
  (let ((x (loop for i in theList
               collect (list i Delimiter))))
    (format t "~:{~a ~a ~}" x)))

Maybe I could get by without using loop at all, and without creating
that other list x? Just wondering.
-KLM

PS. also tried, successfully:

(defun ListPrint2 (Delimiter theList)
  (loop for foo in theList
      for i from 1
      do (format t "~a " foo)
      when (< i (length theList))
      do (format t "~a " Delimiter)))

From: Mattias Nilsson
Subject: Re: format question
Date: 
Message-ID: <1154636911.722233.127120@h48g2000cwc.googlegroups.com>
·······@gmail.com wrote:
> I'm creating a function that prints a list with a particular delimiter
> (character). The arguments to the function are theList and Delimiter. I
> can think of a solution using loop, but I was wondering if it was
> possible to also use only the format directives.

Yes. You can use format twice, and some ugly double ~'s:

(defun list-print (delimiter list)
  (format t (format nil "~~{~~A~~^ ~A ~~}" delimiter) list))

Mattias
From: Thomas A. Russ
Subject: Re: format question
Date: 
Message-ID: <ymislkdwclk.fsf@sevak.isi.edu>
"Mattias Nilsson" <········@bredband.net> writes:

> ·······@gmail.com wrote:
> > I'm creating a function that prints a list with a particular delimiter
> > (character). The arguments to the function are theList and Delimiter. I
> > can think of a solution using loop, but I was wondering if it was
> > possible to also use only the format directives.
> 
> Yes. You can use format twice, and some ugly double ~'s:
> 
> (defun list-print (delimiter list)
>   (format t (format nil "~~{~~A~~^ ~A ~~}" delimiter) list))
> 
> Mattias

And just to help out newbie Kyle, I'll show the basic format directive
used by Mattias, for the case where the delimiter is fixed as ", "n

    (format t "~{~A~^, ~}" list)

This uses the ~{ ... ~} list iteration construct, with the "~^" escape
construct inside it, which stops processing once the list runs out.

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Nathan Baum
Subject: Re: format question
Date: 
Message-ID: <Pine.LNX.4.64.0608032127070.24883@localhost>
On Thu, 3 Aug 2006, ·······@gmail.com wrote:

> Hi, I am a newbie and I'm learning about format directives. I'm
> creating a function that prints a list with a particular delimiter
> (character). The arguments to the function are theList and Delimiter. I
> can think of a solution using loop, but I was wondering if it was
> possible to also use only the format directives. Here's my code (works
> except it throws one last delimiter at the end):
>
> (defun ListPrint (Delimiter theList)
>  (let ((x (loop for i in theList
>               collect (list i Delimiter))))
>    (format t "~:{~a ~a ~}" x)))
>
> Maybe I could get by without using loop at all, and without creating
> that other list x? Just wondering.
> -KLM

(Gotta say I hate the PascalCase...)

(defun list-print (list &optional (delimiter " ")
                                   (stream *standard-output*))
   "Print list with delimiter on stream."
   (when list
     (princ (car list) stream)
     (dolist (node (cdr list))
       (princ delimiter stream)
       (princ node stream))))

(defun list-print (list &optional (delimiter " ")
                                   (stream *standard-output*))
   "Print list with delimiter on stream.
    Delimiter may contain format directives."
   (format stream (format nil "~~{~~A~~^~A~~}" delimiter) list))

> PS. also tried, successfully:
>
> (defun ListPrint2 (Delimiter theList)
>  (loop for foo in theList
>      for i from 1
>      do (format t "~a " foo)
>      when (< i (length theList))
>      do (format t "~a " Delimiter)))
>
>