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>"))
··········@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.
"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")
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
> 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
+ "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
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.
+ 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
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.
>
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 ~}".
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
+ 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
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
"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
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>"))
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