emacs.d/clones/lisp/colinallen.dnsalias.org/lp/node60.html

125 lines
4.8 KiB
HTML
Raw Normal View History

2022-08-02 12:34:59 +02:00
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Originally converted to HTML using LaTeX2HTML 95 (Thu Jan 19 1995) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE> Nicer Output Using Format</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Nicer Output Using Format">
<meta name="keywords" value="lp">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR>
<A HREF="node61.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
<A HREF="node58.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
<A HREF="node59.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <BR>
<A HREF="lp.html"><B>Contents</B></A>
<B> Next:</B>
<A HREF="node61.html"> Reading</A>
<B>Up:</B>
<A HREF="node58.html"> Input and Output</A>
<B> Previous:</B>
<A HREF="node59.html"> Basic Printing</A>
<BR> <HR> <P>
<H1> Nicer Output Using Format</H1>
<P>
Print and its kin are useful basic printing functions, but for sophisticated and easy to read output, format is more useful.
<P>
The basic structure of a format call is
<P>
<BLOCKQUOTE>
<PRE> (format &lt;destination&gt; &lt;control-string&gt; &lt;optional-arguments&gt; )
</PRE>
</BLOCKQUOTE>
The full use of a destination will be introduced further below; for most basic uses, the destination should be specified as t or nil. The control string is a string containing characters to be printed, as well as control sequences. Every control sequence begins with a tilde: <code>~</code>. The control sequences may require extra arguments to be evaluated, which must be provided as optional arguments to format.
<P>
With t as the specified destination, and no control sequences in the control-string, format outputs the string in a manner similar to princ, and returns nil.
<P>
<BLOCKQUOTE>
<PRE>&gt; (format t &quot;this&quot;)
this
NIL
</PRE>
</BLOCKQUOTE>
With nil as destination and no control sequences, format simply returns the string:
<P>
<BLOCKQUOTE>
<PRE>&gt; (format nil &quot;this&quot;)
&quot;this&quot;
</PRE>
</BLOCKQUOTE>
Inserting <code>~</code>% in the control string causes a newline to be output:
<P>
<BLOCKQUOTE>
<PRE>&gt; (format t &quot;~%This shows ~%printing with ~%newlines.~%&quot;)
This shows
printing with
newlines.
NIL
</PRE>
</BLOCKQUOTE>
<code>~</code>s indicates that an argument is to be evaluated and the result inserted at that point. Each <code>~</code>s in the control string must match up to an optional argument appearing after the control string.
<P>
Here is an example of a function that uses this capability:
<P>
<BLOCKQUOTE>
<PRE>(defun f-to-c (ftemp)
(let ((ctemp (* (- ftemp 32) 5/9)))
(format t
&quot;~%~s degrees Fahrenheit is ~%~s degrees Celsius~%&quot;
ftemp ;; first ~s
(float ctemp)) ;; second ~s
ctemp)) ;; return ratio value
</PRE>
</BLOCKQUOTE>
(Float converts a number from integer or ratio to a floating point number, i.e. one with a decimal point.)
<P>
<BLOCKQUOTE>
<PRE>&gt; (f-to-c 82)
82 degrees Fahrenheit is
27.777777777777777 degrees Celsius
250/9
</PRE>
</BLOCKQUOTE>
Format simply evaluates the optional arguments in order they appear to determine the values for each <code>~</code>s. There must be enough optional arguments following the control string to provide values for all the occurrences of <code>~</code>s in the control string, otherwise an error will occur. If there are too many optional arguments, format evaluates them all (so side effects may occur) but no error is signalled.
<P>
Format will also preserve newlines entered directly in the control string. For example, f-to-c would behave just the same if defined as follows:
<P>
<BLOCKQUOTE>
<PRE>(defun f-to-c (ftemp)
(let ((ctemp (* (- ftemp 32) 5/9)))
(format t &quot;
~s degrees Fahrenheit is
~s degrees Celsius
&quot;
ftemp
(float ctemp)) ;; print floated value
ctemp)) ;; return ratio value
</PRE>
</BLOCKQUOTE>
In addition to these two control sequences it is useful to know about <code>~</code>T to produce tab spacing and <code>~</code><code>~</code> to print a tilde. Some other control sequences are documented in the appendix entry for format.
<P>
<BR> <HR>
<A HREF="node61.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
<A HREF="node58.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
<A HREF="node59.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <BR>
<A HREF="lp.html"><B>Contents</B></A>
<B> Next:</B>
<A HREF="node61.html"> Reading</A>
<B>Up:</B>
<A HREF="node58.html"> Input and Output</A>
<B> Previous:</B>
<A HREF="node59.html"> Basic Printing</A>
<BR> <HR> <P>
<BR> <HR>
<P>
<ADDRESS>
<I>&#169; Colin Allen &amp; Maneesh Dhagat <BR>
March 2007 </I>
</ADDRESS>
</BODY>