Liking cljdoc? Tell your friends :D

22.3.3. Formatted Output to Character Streams

Common Lisp the Language, 2nd Edition


Overview

The format function produces formatted output to character streams or returns formatted strings. Control strings use tildes (~) to introduce directives that specify formatting operations.

Basic Function

[Function] format destination control-string &rest arguments

The function outputs characters from control-string, with tildes introducing directives. A directive consists of:

  • A tilde (~)
  • Optional prefix parameters (comma-separated integers)
  • Optional colon (:) and at-sign (@) modifiers
  • A single character indicating directive type

When destination is nil, format returns a string. When destination is t, output goes to *standard-output*. Otherwise, output is sent to the specified stream or string with fill pointer.

Common Directives

~A (Ascii)

Prints any Lisp object without escape characters (like princ). Supports padding parameters: ~mincol_A adds right spaces; @ modifier adds left spaces.

~S (S-expression)

Similar to ~A but prints with escape characters (like prin1), suitable for read input.

~D (Decimal)

Prints integers in decimal radix. Form: ~mincol_,_padchar_,_commachar_D. The @ modifier prints the sign always; : adds comma separators.

~B, ~O, ~X (Binary, Octal, Hexadecimal)

Print in radix 2, 8, and 16 respectively, with same parameters as ~D.

~R (Radix)

With parameter: ~n_R prints in radix n. Without parameter: ~R prints English cardinal numbers; ~:R prints ordinals; ~@R prints Roman numerals; ~:@R prints old Roman numerals.

~F (Fixed-format floating-point)

Form: ~w_,_d_,_k_,_overflowchar_,_padchar_F

  • w: field width
  • d: digits after decimal point
  • k: scale factor (default 0)

~E (Exponential floating-point)

Form: ~w_,_d_,_e_,_k_,_overflowchar_,_padchar_,_exponentchar_E

Prints in exponential notation with e digits for exponent (default scale factor is 1).

~G (General floating-point)

Automatically chooses between ~F and ~E based on magnitude.

~$ (Dollars)

Form: ~d_,_n_,_w_,_padchar_$

Prints floating-point in fixed notation for currency. Default: 2 digits after decimal, 1 before.

~P (Plural)

Prints lowercase 's' if argument is not eql to 1. ~:P backs up one argument; ~@P prints 'y' or 'ies'.

~C (Character)

Prints a character. ~C uses implementation-dependent format; ~:C spells out control bits and names; ~@C uses #\ syntax.

~%, ~&, ~|, ~~ (Whitespace/Special)

  • ~%: outputs newline(s)
  • ~&: outputs newline unless at line start
  • ~|: outputs page separator
  • ~~: outputs tilde(s)

~T (Tabulate)

Form: ~colnum_,_colinc_T

Spaces to column colnum. If already past it, spaces to colnum+k*colinc. ~@T performs relative tabulation.

~* (Argument Processing)

  • ~*: ignores next argument
  • ~:*: backs up one argument
  • ~@*: goes to nth argument (0 = first)

~? (Indirection)

Processes next argument as control string using following argument's list as arguments.

Advanced Directives

~(str~) (Case Conversion)

  • ~(: lowercase
  • ~:(: capitalize words
  • ~@(: capitalize first word only
  • ~:@(: uppercase

~[str0~;str1~;_...~] (Conditional)

Selects clause based on argument value. ~:; provides default clause. ~:[ selects based on nil/non-nil.

~{str~} (Iteration)

Iterates over list. ~:{ iterates over sublists; ~@{ uses remaining arguments; ~:@{ combines both.

~<str~> (Justification)

Form: ~mincol_,_colinc_,_minpad_,_padchar_<_str_~>

Justifies text in field. : adds spacing before first segment; @ adds spacing after last.

~^ (Up and Out)

Escapes from enclosing ~{ or ~< construct. Can test for remaining arguments with parameters.

Printer Control Variables

Most directives bind relevant printer control variables during processing. For example:

  • ~A binds *print-escape* to nil
  • ~S binds *print-escape* to t
  • ~D, ~B, ~O, ~X bind *print-base* appropriately

Examples

(format nil "foo") => "foo"

(format nil "The answer is ~D." 5) => "The answer is 5."

(format nil "The answer is ~3D." 5) => "The answer is   5."

(format nil "~D item~:P found." 3) => "3 items found."

(format nil "~10<foo~;bar~>") => "foo    bar"

(format nil "~{~S~^ ~}" '(a b c)) => "A B C"

Source: https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node200.html

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close