emacs.d/clones/lisp/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node258.html

73 lines
4.5 KiB
HTML
Raw Normal View History

2022-08-26 19:11:35 +02:00
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>27.5. Compiling Format Control Strings</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Compiling Format Control Strings">
<meta name="keywords" value="clm">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<b>Common Lisp the Language, 2nd Edition</b>
<BR> <HR><A NAME=tex2html4788 HREF="node259.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html4786 HREF="node253.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html4780 HREF="node257.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html4790 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html4791 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html4789 HREF="node259.html"> Pretty Printing Dispatch </A>
<B>Up:</B> <A NAME=tex2html4787 HREF="node253.html"> Pretty Printing</A>
<B> Previous:</B> <A NAME=tex2html4781 HREF="node257.html"> Format Directive Interface</A>
<HR> <P>
<H1><A NAME=SECTION003150000000000000000>27.5. Compiling Format Control Strings</A></H1>
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
The control strings used by <tt>format</tt> are essentially programs that
perform printing. The macro <tt>formatter</tt> provides the efficiency of
using a compiled function for printing without losing the visual compactness of
<tt>format</tt> strings.
<P>
<BR><b>[Macro]</b><BR>
<tt>formatter <i>control-string</i></tt><P>The <i>control-string</i> must be a literal string. An error is signaled if
<i>control-string</i> is not a valid <tt>format</tt> control string. The macro
<tt>formatter</tt> expands into an expression of the form
<tt>(function (lambda (stream &amp;rest args) ...))</tt> that does the printing
specified by <i>control-string</i>. The <tt>lambda</tt> created accepts an
output stream as its first argument and zero or more data values as its
remaining arguments. The value returned by the <tt>lambda</tt> is the tail (if
any) of the data values that are not printed out by <i>control-string</i>.
(For example, if the <i>control-string</i> is <tt>&quot;~A~A&quot;</tt>, the
<tt>cddr</tt> (if any) of the data values is returned.) The form
<tt>(formatter &quot;~%~2@{~S, ~}&quot;)</tt> is
equivalent to the following:
<P><pre>
#'(lambda (stream &amp;rest args)
(terpri stream)
(dotimes (n 2)
(if (null args) (return nil))
(prin1 (pop args) stream)
(write-string &quot;, &quot; stream))
args)
</pre><P>
<P>
In support of the above mechanism, <tt>format</tt> is extended so that it accepts
functions as its second argument as well as strings. When a function is
provided, it must be a function of the form created by <tt>formatter</tt>. The
function is called with the appropriate output stream as its first argument
and the data arguments to <tt>format</tt> as its remaining arguments. The
function should perform whatever output is necessary and return the unused
tail of the arguments (if any). The directives <tt></tt> and
<tt>~{~}</tt> with no body are also extended so that they accept
functions as well as control strings. Every other standard function that
takes a <tt>format</tt> string as an argument (for example, <tt>error</tt> and <tt>warn</tt>)
is also extended so that it can accept functions of the form above
instead.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR> <HR><A NAME=tex2html4788 HREF="node259.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html4786 HREF="node253.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html4780 HREF="node257.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html4790 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html4791 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html4789 HREF="node259.html"> Pretty Printing Dispatch </A>
<B>Up:</B> <A NAME=tex2html4787 HREF="node253.html"> Pretty Printing</A>
<B> Previous:</B> <A NAME=tex2html4781 HREF="node257.html"> Format Directive Interface</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>