Skip to main content

write, prin1, print, pprint, princ

write, prin1, print, pprint, princ Function

Syntax:

write object &key array base case circle escape gensym

length level lines miser-width pprint-dispatch

pretty radix readably right-margin stream

→ object

prin1 object &optional output-stream → object

princ object &optional output-stream → object

write, prin1, print, pprint, princ

print object &optional output-stream → object

pprint object &optional output-stream → ⟨no values⟩

Arguments and Values:

object—an object.

output-stream—an output stream designator . The default is standard output.

array—a generalized boolean.

base—a radix .

case—a symbol of type (member :upcase :downcase :capitalize).

circle—a generalized boolean.

escape—a generalized boolean.

gensym—a generalized boolean.

length—a non-negative integer , or nil.

level—a non-negative integer , or nil.

lines—a non-negative integer , or nil.

miser-width—a non-negative integer , or nil.

pprint-dispatch—a pprint dispatch table.

pretty—a generalized boolean.

radix—a generalized boolean.

readably—a generalized boolean.

right-margin—a non-negative integer , or nil.

stream—an output stream designator . The default is standard output.

Description:

write, prin1, princ, print, and pprint write the printed representation of object to output-stream.

write is the general entry point to the Lisp printer . For each explicitly supplied keyword parameter named in Figure 22–7, the corresponding printer control variable is dynamically bound to its value while printing goes on; for each keyword parameter in Figure 22–7 that is not explicitly supplied, the value of the corresponding printer control variable is the same as it was at the time write was invoked. Once the appropriate bindings are established, the object is output by the Lisp printer .

write, prin1, print, pprint, princ

|Parameter Corresponding Dynamic Variable|

| :- |

|

array *print-array*

base *print-base*

case *print-case*

circle *print-circle*

escape *print-escape*

gensym *print-gensym*

length *print-length*

level *print-level*

lines *print-lines*

miser-width *print-miser-width*

pprint-dispatch *print-pprint-dispatch*

pretty *print-pretty*

radix *print-radix*

readably *print-readably*

right-margin *print-right-margin*

|

Figure 22–7. Argument correspondences for the WRITE function.

prin1, princ, print, and pprint implicitly bind certain print parameters to particu lar values. The remaining parameter values are taken from *print-array*, *print-base*, *print-case*, *print-circle*, *print-escape*, *print-gensym*, *print-length*, *print-level*, *print-lines*, *print-miser-width*, *print-pprint-dispatch*, *print-pretty*, *print-radix*, and *print-right-margin*.

prin1 produces output suitable for input to read. It binds *print-escape* to true.

princ is just like prin1 except that the output has no escape characters. It binds *print-escape* to false and *print-readably* to false. The general rule is that output from princ is intended to look good to people, while output from prin1 is intended to be acceptable to read.

print is just like prin1 except that the printed representation of object is preceded by a newline and followed by a space.

pprint is just like print except that the trailing space is omitted and object is printed with the *print-pretty* flag non-nil to produce pretty output.

Output-stream specifies the stream to which output is to be sent.

Affected By:

*standard-output*, *terminal-io*, *print-escape*, *print-radix*, *print-base*, *print-circle*, *print-pretty*, *print-level*, *print-length*, *print-case*, *print-gensym*, *print-array*, *read-default-float-format*.

See Also:

readtable-case, Section 22.3.4 (FORMAT Printer Operations)

Notes:

The functions prin1 and print do not bind *print-readably*.

(prin1 object output-stream)

(write object :stream output-stream :escape t)

(princ object output-stream)

(write object stream output-stream :escape nil :readably nil)

(print object output-stream)

(progn (terpri output-stream)

(write object :stream output-stream

:escape t)

(write-char #\space output-stream))

(pprint object output-stream)

(write object :stream output-stream :escape t :pretty t)

Expanded Reference: write, prin1, print, pprint, princ

Basic Usage of write

write is the most general output function. It accepts keyword arguments that correspond to printer control variables.

;; write returns the object and prints to *standard-output*
(write 42)
.. 42
=> 42

;; Using keyword arguments to control printing
(write 255 :base 16 :radix t)
.. #xFF
=> 255

prin1 -- Readable Output

prin1 prints an object so it can be read back by read. It binds *print-escape* to true.

(prin1 "hello")
.. "hello"
=> "hello"

(prin1 'foo)
.. FOO
=> FOO

;; Strings include quotes, characters include #\ prefix
(prin1-to-string #\Space)
=> "#\\ "

princ -- Human-Readable Output

princ prints without escape characters. It binds *print-escape* to false and *print-readably* to false.

(princ "hello")
.. hello
=> "hello"

(princ 'foo)
.. FOO
=> FOO

;; Compare prin1 vs princ for strings
(prin1-to-string "hello")
=> "\"hello\""

(princ-to-string "hello")
=> "hello"

print outputs a newline before and a space after the object. Like prin1, it uses escape characters.

;; print outputs: newline, then object, then space
(progn (print 42) (print 43) nil)
;; ..
;; .. 42
;; .. 43
;; => NIL

pprint -- Pretty-Printed Output

pprint is like print but uses the pretty printer and does not add a trailing space. Returns no values.

(pprint '(defun factorial (n)
(if (<= n 1)
1
(* n (factorial (1- n))))))
..
.. (DEFUN FACTORIAL (N)
.. (IF (<= N 1)
.. 1
.. (* N (FACTORIAL (1- N)))))
=> ; No value
;; write with :pretty t is equivalent to pprint
(write '(let ((a 1) (b 2) (c 3)) (+ a b c)) :pretty t)
.. (LET ((A 1) (B 2) (C 3))
.. (+ A B C))
=> (LET ((A 1) (B 2) (C 3)) (+ A B C))

write with Multiple Keyword Arguments

;; Combine multiple print-control settings
(write '(a b c) :pretty nil :case :downcase)
.. (a b c)
=> (A B C)

(write 42 :base 2 :radix t)
.. #b101010
=> 42