From: Szymon
Subject: FORMAT question.
Date: 
Message-ID: <85fz2dxfwj.fsf@eva.rplacd.net>
Hi.

How format-string should look like?

(format t format-string 1 2 3)

==> 1 2 3 2 1

Is this possible?

Regards, Szymon.

From: David Sletten
Subject: Re: FORMAT question.
Date: 
Message-ID: <hQAud.2127$gd.609@twister.socal.rr.com>
Szymon wrote:

> Hi.
> 
> How format-string should look like?
> 
> (format t format-string 1 2 3)
> 
> ==> 1 2 3 2 1
> 
> Is this possible?
> 
> Regards, Szymon.
(format t "~D ~D ~D ~2:*~D ~2:*~D" 1 2 3)
From: Szymon
Subject: Re: FORMAT question.
Date: 
Message-ID: <858y84yie0.fsf@eva.rplacd.net>
Many thanks for the replies :)

Final result

··············@<~A~>·····@<~A~>····@*~4:<~A~>······@·······@*~A~%"

;)

Regards, Szymon.
From: http://public.xdi.org/=pf
Subject: Re: FORMAT question.
Date: 
Message-ID: <m2vfb9f4g1.fsf@mycroft.actrix.gen.nz>
On Sat, 11 Dec 2004 11:55:56 +0100, Szymon  wrote:

> Hi.

> How format-string should look like?

> (format t format-string 1 2 3)

> ==> 1 2 3 2 1

> Is this possible?

Sure.  "~S ~S ~S ~:2*~S ~:2*~S"

-- 
If that makes any sense to you, you have a big problem.
                                      -- C. Durance, Computer Science 234
(setq reply-to
  (concatenate 'string "Paul Foley " "<mycroft" '(··@) "actrix.gen.nz>"))
From: ··········@gmail.com
Subject: Re: FORMAT question.
Date: 
Message-ID: <1102777914.628718.230610@f14g2000cwb.googlegroups.com>
i'm not sure how exactly does format works :| .. can some one show me
some simple examples as to how it works? thanks alot!
From: Pascal Bourguignon
Subject: Re: FORMAT question.
Date: 
Message-ID: <876538svkk.fsf@thalassa.informatimago.com>
··········@gmail.com writes:

> i'm not sure how exactly does format works :| .. can some one show me
> some simple examples as to how it works? thanks alot!

Easy. Fetch the source of your favorite implementation and read FORMAT:
 
(defun format (destination control-string &rest arguments)
  (unless (or (stringp control-string) (functionp control-string))
    (format-cs-error control-string))
  (cond ((null destination)
         (let ((stream (make-string-output-stream)))
           (format-apply stream control-string arguments)
           (get-output-stream-string stream)))
        ((eq destination 'T)
         (format-apply *standard-output* control-string arguments)
         nil)
        ((streamp destination)
         (format-apply destination control-string arguments)
         nil)
        ((stringp destination)
         (if (array-has-fill-pointer-p destination)
           (let ((stream (sys::make-string-push-stream destination)))
             (format-apply stream control-string arguments))
           (error-of-type 'error
             (TEXT "The destination string ~S should have a fill pointer.")
             destination))
         nil)
        (t (error-of-type 'type-error
             :datum destination :expected-type '(or boolean stream string)
             (TEXT "The destination argument ~S is invalid (not NIL or T or a stream or a string).")
             destination))))

So, FORMAT basically works by testing the destination and using FORMAT-APPLY.
And FORMAT-APPLY works as follow:

(defun format-apply (stream control-string arguments
                     &optional (whole-arguments arguments))
  (cond ((stringp control-string)
         ;; possibly convert control-string into Simple-String ??
         (let ((node (list control-string)))
           (format-parse-cs control-string 0 node nil)
           (let* ((*FORMAT-CS*         (car node))
                  (*FORMAT-CSDL*       (cdr node))
                  (*FORMAT-ARG-LIST*   whole-arguments)
                  (*FORMAT-NEXT-ARG*   arguments)
                  (*FORMAT-NEXT-ARGLIST* nil)
                  (*FORMAT-UP-AND-OUT* nil))
             (format-interpret stream)
             *FORMAT-NEXT-ARG*)))
        ((functionp control-string)
         (let ((*FORMAT-CS* nil)) ; format-error cannot point to the position anymore
           (apply control-string stream arguments)))
        (t (format-cs-error control-string))))

That is: it parses the control string, and then it interprets it.

To further see examples of the working of FORMAT, I would use:

    grep -i defun format.lisp|awk '{print $2}'

to get the list of functions in the format module, and apply TRACE on this list:
(trace format format-apply ...)

then I'd try some examples:

(format nil "Hello ~A" "John")

and Lisp would show me how format works.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
The world will now reboot; don't bother saving your artefacts.
From: Coby Beck
Subject: Re: FORMAT question.
Date: 
Message-ID: <vUFud.53359$6f6.9720@edtnps89>
"Pascal Bourguignon" <····@mouse-potato.com> wrote in message 
···················@thalassa.informatimago.com...
> ··········@gmail.com writes:
>
>> i'm not sure how exactly does format works :| .. can some one show me
>> some simple examples as to how it works? thanks alot!
>
> Easy. Fetch the source of your favorite implementation and read FORMAT:

Or better yet, check the hyperspec's formatted output section:
http://www.lispworks.com/reference/HyperSpec/Body/22_c.htm


-- 
Coby Beck
(remove #\Space "coby 101 @ big pond . com")
From: Chris Capel
Subject: Re: FORMAT question.
Date: 
Message-ID: <10rpqept6742n8b@corp.supernews.com>
Pascal Bourguignon wrote:

> ··········@gmail.com writes:
> 
>> i'm not sure how exactly does format works :| .. can some one show me
>> some simple examples as to how it works? thanks alot!
> 
> Easy. Fetch the source of your favorite implementation and read FORMAT:

Ehh. I'd try a book. I've found that Graham's ANSI Common Lisp has a good
quick reference on format.

Personally, I've never gone beyond using ~A and ~% in my format strings. If
I had to do more, I'd write a replacement output language using stream
primitives, or go find one. I find format repugnant.

Chris Capel
From: Bruno Haible
Subject: Re: FORMAT question.
Date: 
Message-ID: <cpko9g$1m7$1@laposte.ilog.fr>
> Personally, I've never gone beyond using ~A and ~% in my format strings.

You also need ~S when you produce output for a programmer.

Internationalization needs ~*.

And when you want to mix code which uses  "Some line of text.~%"  with
other code which uses  "~%A line in old newline-before style.", you
also need ~&.

Bruno
From: Steven M. Haflich
Subject: Re: FORMAT question.
Date: 
Message-ID: <m%Hud.41372$6q2.17479@newssvr14.news.prodigy.com>
Paul Foley (http://public.xdi.org/=pf) wrote:

>>(format t format-string 1 2 3)
>>==> 1 2 3 2 1
> Sure.  "~S ~S ~S ~:2*~S ~:2*~S"

Much too complicated and hard to understand.  Simpler solution:

(format t "1 2 3 2 1" 1 2 3)
==> 1 2 3 2 1
From: David Sletten
Subject: Re: FORMAT question.
Date: 
Message-ID: <oKIud.2155$gd.717@twister.socal.rr.com>
Steven M. Haflich wrote:

> Paul Foley (http://public.xdi.org/=pf) wrote:
> 
>>> (format t format-string 1 2 3)
>>> ==> 1 2 3 2 1
>>
>> Sure.  "~S ~S ~S ~:2*~S ~:2*~S"
> 
> 
> Much too complicated and hard to understand.  Simpler solution:
> 
> (format t "1 2 3 2 1" 1 2 3)
> ==> 1 2 3 2 1

Bravo!
From: Jim Newton
Subject: Re: FORMAT question.
Date: 
Message-ID: <321go7F3fnfdpU1@individual.net>
David Sletten wrote:
> Steven M. Haflich wrote:
> 
>> Paul Foley (http://public.xdi.org/=pf) wrote:
>>
>>>> (format t format-string 1 2 3)
>>>> ==> 1 2 3 2 1
>>>
>>>
>>> Sure.  "~S ~S ~S ~:2*~S ~:2*~S"
>>
>>
>>
>> Much too complicated and hard to understand.  Simpler solution:
>>
>> (format t "1 2 3 2 1" 1 2 3)
>> ==> 1 2 3 2 1
>

how about

     (format t "~D ~D ~D 2 1" 1 2 3)
From: Harald Hanche-Olsen
Subject: Re: FORMAT question.
Date: 
Message-ID: <pco1xdvenvz.fsf@shuttle.math.ntnu.no>
+ "Steven M. Haflich" <·················@alum.mit.edu>:

| Paul Foley (http://public.xdi.org/=pf) wrote:
| 
| >>(format t format-string 1 2 3)
| >>==> 1 2 3 2 1
| > Sure.  "~S ~S ~S ~:2*~S ~:2*~S"
| 
| Much too complicated and hard to understand.  Simpler solution:
| 
| (format t "1 2 3 2 1" 1 2 3)
| ==> 1 2 3 2 1

Funny.  But to get at least semiserious: Is it possible to write a
format string to print an arbitrary number of arguments in reverse
order?

I think not, but I am willing to be surprised.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- Debating gives most of us much more psychological satisfaction
  than thinking does: but it deprives us of whatever chance there is
  of getting closer to the truth.  -- C.P. Snow
From: Svein Ove Aas
Subject: Re: FORMAT question.
Date: 
Message-ID: <cphd37$ca7$1@services.kq.no>
Harald Hanche-Olsen wrote:

> Funny.  But to get at least semiserious: Is it possible to write a
> format string to print an arbitrary number of arguments in reverse
> order?
> 
> I think not, but I am willing to be surprised.
> 
Well... you could always write a custom format function.
Sick, I know, but definitely *possible*. I certainly wouldn't want anyone to
change the argument list around a bit instead.
From: Harald Hanche-Olsen
Subject: Re: FORMAT question.
Date: 
Message-ID: <pcomzwjwvhr.fsf@shuttle.math.ntnu.no>
+ Svein Ove Aas <·········@aas.no>:

| Harald Hanche-Olsen wrote:
| 
| > Funny.  But to get at least semiserious: Is it possible to write a
| > format string to print an arbitrary number of arguments in reverse
| > order?
| > 
| > I think not, but I am willing to be surprised.
| > 
| Well... you could always write a custom format function.
| Sick, I know, but definitely *possible*.

But that's not portable CL, right?

| I certainly wouldn't want anyone to change the argument list around
| a bit instead.

I thought of this sort of as a puzzle, like the ones you find in some
Sunday papers.  Also, it seems more line with the intent of the OP.
But I would never seriously consider /using/ such a format string.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- Debating gives most of us much more psychological satisfaction
  than thinking does: but it deprives us of whatever chance there is
  of getting closer to the truth.  -- C.P. Snow
From: Svein Ove Aas
Subject: Re: FORMAT question.
Date: 
Message-ID: <cphg1r$292$1@services.kq.no>
Harald Hanche-Olsen wrote:

> + Svein Ove Aas <·········@aas.no>:
> 
> | Harald Hanche-Olsen wrote:
> | 
> | > Funny.  But to get at least semiserious: Is it possible to write a
> | > format string to print an arbitrary number of arguments in reverse
> | > order?
> | > 
> | > I think not, but I am willing to be surprised.
> | > 
> | Well... you could always write a custom format function.
> | Sick, I know, but definitely *possible*.
> 
> But that's not portable CL, right?
> 
Sure it is; check the hyperspec.
Try the following:

(format t
   (lambda (s &rest args)
     (format s "~{~D~} ~{~D~}"
       args
       (cdr (reverse args))))
    1 2 3 4 'anything 'in 'a 'list)

> | I certainly wouldn't want anyone to change the argument list around
> | a bit instead.
> 
> I thought of this sort of as a puzzle, like the ones you find in some
> Sunday papers.  Also, it seems more line with the intent of the OP.
> But I would never seriously consider /using/ such a format string.
> 
From: Svein Ove Aas
Subject: Re: FORMAT question.
Date: 
Message-ID: <cphg6k$292$2@services.kq.no>
Svein Ove Aas wrote:

> Sure it is; check the hyperspec.
> Try the following:
> 
> (format t
>    (lambda (s &rest args)
>      (format s "~{~D~} ~{~D~}"
>        args
>        (cdr (reverse args))))
>     1 2 3 4 'anything 'in 'a 'list)
> 

The format-string here should of course be
"~{~D ~}~{~D ~}".
From: Rob Warnock
Subject: Re: FORMAT question.
Date: 
Message-ID: <UfqdnTFgjqMSOEXcRVn-tA@speakeasy.net>
Svein Ove Aas  <·········@aas.no> wrote:
+---------------
| Svein Ove Aas wrote:
| > (format t
| >    (lambda (s &rest args)
| >      (format s "~{~D~} ~{~D~}"
| >        args
| >        (cdr (reverse args))))
| >     1 2 3 4 'anything 'in 'a 'list)
| 
| The format-string here should of course be
| "~{~D ~}~{~D ~}".
+---------------

I beg to differ! ;-}  That one produces a dangling space at the end.
The correct format string is obviously either:

    "~{~D ~}~{~D~^ ~}"

or if you prefer [for parallelism]:

    "~{~D~^ ~} ~{~D~^ ~}"


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Harald Hanche-Olsen
Subject: Re: FORMAT question.
Date: 
Message-ID: <pcooegzh91k.fsf@shuttle.math.ntnu.no>
+ Svein Ove Aas <·········@aas.no>:

| Harald Hanche-Olsen wrote:
| 
| > + Svein Ove Aas <·········@aas.no>:
| > 
| > | Well... you could always write a custom format function.
| > | Sick, I know, but definitely *possible*.
| > 
| > But that's not portable CL, right?
| > 
| Sure it is; check the hyperspec.

Oh, indeed.  One of these days I ought to go through the whole darn
thing and find out what else I have been missing.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- Debating gives most of us much more psychological satisfaction
  than thinking does: but it deprives us of whatever chance there is
  of getting closer to the truth.  -- C.P. Snow
From: Paul F. Dietz
Subject: Re: FORMAT question.
Date: 
Message-ID: <fYadneR1yNOMwyHcRVn-uQ@dls.net>
Harald Hanche-Olsen wrote:

> Oh, indeed.  One of these days I ought to go through the whole darn
> thing and find out what else I have been missing.


(cackles evilly)

	Paul
From: Carl Taylor
Subject: Re: FORMAT question.
Date: 
Message-ID: <6T5vd.1082574$Gx4.925502@bgtnsc04-news.ops.worldnet.att.net>
"Harald Hanche-Olsen" <······@math.ntnu.no> wrote in message 
····················@shuttle.math.ntnu.no...
>+ "Steven M. Haflich" <·················@alum.mit.edu>:
>
> | Paul Foley (http://public.xdi.org/=pf) wrote:
> |
> | >>(format t format-string 1 2 3)
> | >>==> 1 2 3 2 1
> | > Sure.  "~S ~S ~S ~:2*~S ~:2*~S"
> |
> | Much too complicated and hard to understand.  Simpler solution:
> |
> | (format t "1 2 3 2 1" 1 2 3)
> | ==> 1 2 3 2 1
>
> Funny.  But to get at least semiserious: Is it possible to write a
> format string to print an arbitrary number of arguments in reverse
> order?
>
> I think not, but I am willing to be surprised.

This is dirty as sin and illegitimate as it traps a serious error condition 
as part of its processing, but  it works.  Of course maybe you wanted an 
unmassaged argument list going into the <format> form??

(defun foo (in-list)
   (let* ((llng (length in-list))
          (v1-list (nreverse
                     (loop for n from 1 by 3 repeat llng collecting n)))
          (v2-list (loop for m from 2 by 3 repeat llng collecting m))
          (arg-list (concatenate 'list
                                 v1-list
                                 (mapcan #'list in-list v2-list))))
     (format t "~%arg-list = ~A~%" arg-list)
     (handler-case
        (format t ·······@*~A ~V:*~}" arg-list)
        (serious-condition () (values)))))


CL-USER 98 > (foo '(a b c d e f g))

arg-list = (19 16 13 10 7 4 1 A 2 B 5 C 8 D 11 E 14 F 17 G 20)

G F E D C B A

Carl Taylor
From: http://public.xdi.org/=pf
Subject: Re: FORMAT question.
Date: 
Message-ID: <m2r7lvf901.fsf@mycroft.actrix.gen.nz>
On 12 Dec 2004 12:50:40 +0100, Harald Hanche-Olsen wrote:

> + "Steven M. Haflich" <·················@alum.mit.edu>:
> | Paul Foley (http://public.xdi.org/=pf) wrote:
> | 
> | >>(format t format-string 1 2 3)
> | >>==> 1 2 3 2 1
> | > Sure.  "~S ~S ~S ~:2*~S ~:2*~S"
> | 
> | Much too complicated and hard to understand.  Simpler solution:
> | 
> | (format t "1 2 3 2 1" 1 2 3)
> | ==> 1 2 3 2 1

> Funny.  But to get at least semiserious: Is it possible to write a
> format string to print an arbitrary number of arguments in reverse
> order?

> I think not, but I am willing to be surprised.

 (ignore-errors (format t ··@·········@{~S~2:* ~}" 1 2 3 4 5 6))

works.  I don't know how to avoid the error, though.

-- 
If that makes any sense to you, you have a big problem.
                                      -- C. Durance, Computer Science 234
(setq reply-to
  (concatenate 'string "Paul Foley " "<mycroft" '(··@) "actrix.gen.nz>"))
From: Thomas A. Russ
Subject: Re: FORMAT question.
Date: 
Message-ID: <ymik6rk63n5.fsf@sevak.isi.edu>
Harald Hanche-Olsen <······@math.ntnu.no> writes:

> 
> + "Steven M. Haflich" <·················@alum.mit.edu>:
> 
> | Paul Foley (http://public.xdi.org/=pf) wrote:
> | 
> | >>(format t format-string 1 2 3)
> | >>==> 1 2 3 2 1
> | > Sure.  "~S ~S ~S ~:2*~S ~:2*~S"
> | 
> | Much too complicated and hard to understand.  Simpler solution:
> | 
> | (format t "1 2 3 2 1" 1 2 3)
> | ==> 1 2 3 2 1
> 
> Funny.  But to get at least semiserious: Is it possible to write a
> format string to print an arbitrary number of arguments in reverse
> order?
> 
> I think not, but I am willing to be surprised.

Not exactly, but one can get fairly close with the following:

  (format t "~{~S~^ ~}~{ ~S~}" 
          arglist (rest (reverse arglist)))

In the case in question 
 (let ((arglist '(1 2 3)))
  (format t "~{~S~^ ~}~{ ~S~}" 
          arglist (rest (reverse arglist))))

=>  1 2 3 2 1
()


-- 
Thomas A. Russ,  USC/Information Sciences Institute