This chapter covers Lisp's FORMAT function, a text formatting system inspired by FORTRAN that implements its own programming language for output formatting.
The FORMAT function can:
FORMAT requires three components: a destination argument, a format control string, and optional arguments for the control string.
Destination options:
T: sends output to *STANDARD-OUTPUT*NIL: returns formatted output as a string| Directive | Purpose |
|---|---|
~% | newline |
~& | fresh line (newline if not already at line start) |
~\| | page break |
~T | tab stop |
~C | character |
~D | decimal integer |
~B, ~O, ~X | binary, octal, hexadecimal |
~R | spell integer as words |
~P | plural handling |
~F, ~E, ~G, ~$ | floating-point formats |
~A | legible output (no escapes) |
~S | readable output (with escapes) |
~~ | literal tilde |
~(text~): lowercase~:@(text~): uppercase~:(text~): capitalize each word~@(text~): capitalize first word only~wD: right-justify in field width w~w,'cD: pad with character c~@D: include plus sign for positive numbers~:D: insert commas between digit groups~@R converts integers to Roman numeral representation.
~P appends "s"; ~@P handles "y"/"ies"; ~:P reuses previous argument.
Use ~{format-control~} to iterate over list elements. Include ~^ to terminate early if arguments exhaust.
Three forms exist:
~[format-0~;format-1~;...~] selects clause by integer index~:[false~;true~] tests for NIL~@[format~] tests argument; processes only if non-NILUse V to specify parameters from the argument list at runtime, allowing field widths and other values to vary during execution.
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 |