File: LISP-tutorial-8.html

package info (click to toggle)
cmucl 20c-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 42,524 kB
  • sloc: lisp: 358,331; ansic: 28,385; asm: 3,777; sh: 1,236; makefile: 366; csh: 31
file content (71 lines) | stat: -rw-r--r-- 2,642 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<HTML>
<HEAD>
<TITLE>Common LISP Hints: Printing</TITLE>
</HEAD>
<BODY>
<A HREF="LISP-tutorial-7.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="LISP-tutorial-9.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="LISP-tutorial.html#toc8"><IMG SRC="toc.gif" ALT="Contents"></A>
<HR>
<H2><A NAME="s8">8. Printing</A></H2>

<P>Some functions can cause output. The simplest one is <CODE>print</CODE>, which
prints its argument and then returns it.</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
&gt; (print 3)
3
3
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>The first 3 above was printed, the second was returned.</P>
<P>If you want more complicated output, you will need to use <CODE>format</CODE>.
Here's an example:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
&gt; (format t &quot;An atom: ~S~%and a list: ~S~%and an integer: ~D~%&quot;
          nil (list 5) 6)
An atom: NIL
and a list: (5)
and an integer: 6
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>The first argument to format is either <CODE>t</CODE>, <CODE>nil</CODE>, or a
stream. <CODE>T</CODE> specifies 
output to the terminal. <CODE>Nil</CODE> means not to print anything but to return a
string containing the output instead. Streams are general places for
output to go: they can specify a file, or the terminal, or another
program. This handout will not describe streams in any further detail.</P>
<P>The second argument is a formatting template, which is a string
optionally containing formatting directives.</P>
<P>All remaining arguments may be referred to by the formatting
directives. LISP will replace the directives with some appropriate
characters based on the arguments to which they refer and then print
the resulting string.</P>
<P>Format always returns <CODE>nil</CODE> unless its first argument is <CODE>nil</CODE>, in which
case it prints nothing and returns the string instead of printing it.</P>
<P>There are three different directives in the above example:
<CODE>~S</CODE>, <CODE>~D</CODE>, and 
<CODE>~%</CODE>. The first one accepts any LISP object and is
replaced by a printed 
representation of that object (the same representation which is
produced by print). The second one accepts only integers. The third one
doesn't refer to an argument; it is always replaced by a carriage
return.</P>
<P>Another useful directive is <CODE>~~</CODE>, which is replaced by
a single ~. </P>
<P>Refer to a LISP manual for (many, many) additional formatting
directives.</P>



<HR>
<A HREF="LISP-tutorial-7.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="LISP-tutorial-9.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="LISP-tutorial.html#toc8"><IMG SRC="toc.gif" ALT="Contents"></A>
</BODY>
</HTML>