Some functions can cause output. The simplest one is print
, which
prints its argument and then returns it.
> (print 3)
3
3
The first 3 above was printed, the second was returned.
If you want more complicated output, you will need to use format
.
Here's an example:
> (format t "An atom: ~S~%and a list: ~S~%and an integer: ~D~%"
nil (list 5) 6)
An atom: NIL
and a list: (5)
and an integer: 6
The first argument to format is either t
, nil
, or a
stream. T
specifies
output to the terminal. Nil
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.
The second argument is a formatting template, which is a string optionally containing formatting directives.
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.
Format always returns nil
unless its first argument is nil
, in which
case it prints nothing and returns the string instead of printing it.
There are three different directives in the above example:
~S
, ~D
, and
~%
. 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.
Another useful directive is ~~
, which is replaced by
a single ~.
Refer to a LISP manual for (many, many) additional formatting directives.