Common Lisp the Language, 2nd Edition
The format function produces formatted output to character streams or returns formatted strings. Control strings use tildes (~) to introduce directives that specify formatting operations.
[Function]
format destination control-string &rest arguments
The function outputs characters from control-string, with tildes introducing directives. A directive consists of:
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.
Prints any Lisp object without escape characters (like princ). Supports padding parameters: ~mincol_A adds right spaces; @ modifier adds left spaces.
Similar to ~A but prints with escape characters (like prin1), suitable for read input.
Prints integers in decimal radix. Form: ~mincol_,_padchar_,_commachar_D. The @ modifier prints the sign always; : adds comma separators.
Print in radix 2, 8, and 16 respectively, with same parameters as ~D.
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.
Form: ~w_,_d_,_k_,_overflowchar_,_padchar_F
Form: ~w_,_d_,_e_,_k_,_overflowchar_,_padchar_,_exponentchar_E
Prints in exponential notation with e digits for exponent (default scale factor is 1).
Automatically chooses between ~F and ~E based on magnitude.
Form: ~d_,_n_,_w_,_padchar_$
Prints floating-point in fixed notation for currency. Default: 2 digits after decimal, 1 before.
Prints lowercase 's' if argument is not eql to 1. ~:P backs up one argument; ~@P prints 'y' or 'ies'.
Prints a character. ~C uses implementation-dependent format; ~:C spells out control bits and names; ~@C uses #\ syntax.
~%: outputs newline(s)~&: outputs newline unless at line start~|: outputs page separator~~: outputs tilde(s)Form: ~colnum_,_colinc_T
Spaces to column colnum. If already past it, spaces to colnum+k*colinc. ~@T performs relative tabulation.
~*: ignores next argument~:*: backs up one argument~@*: goes to nth argument (0 = first)Processes next argument as control string using following argument's list as arguments.
~(: lowercase~:(: capitalize words~@(: capitalize first word only~:@(: uppercaseSelects clause based on argument value. ~:; provides default clause. ~:[ selects based on nil/non-nil.
Iterates over list. ~:{ iterates over sublists; ~@{ uses remaining arguments; ~:@{ combines both.
Form: ~mincol_,_colinc_,_minpad_,_padchar_<_str_~>
Justifies text in field. : adds spacing before first segment; @ adds spacing after last.
Escapes from enclosing ~{ or ~< construct. Can test for remaining arguments with parameters.
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(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
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |