Format (Common Lisp)
Format
is a function in Common Lisp that can produce formatted text using a format string similar to the printf format string. It provides more functionality than printf
, allowing the user to output numbers in English, apply certain format specifiers only under certain conditions, iterate over data structures, and output in a tabular format. This functionally originates in MIT's Lisp Machine Lisp, where it was based on Multics ioa_
.
Example
An example of a C printf
call is the following:
printf("Color %s, number1 %d, number2 %05d, hex %x, float %5.2f, unsigned value %u.\n",
"red", 123456, 89, 255, 3.14, 250);
Using Common Lisp, this is equivalent to:
(format t "Color ~A, number1 ~D, number2 ~5,'0D, hex ~X, float ~5,2F, unsigned value ~D.~%"
"red" 123456 89 255 3.14 250)
;; prints: Color red, number1 123456, number2 00089, hex FF, float 3.14, unsigned value 250.
Another example would be to print every element of list delimited with commas, which can be done using the ~{, ~^ and ~} directives:[1]
(let ((groceries '(eggs bread butter carrots)))
(format t "~{~A~^, ~}.~%" groceries) ; Prints in uppercase
(format t "~@(~{~A~^, ~}~).~%" groceries)) ; Capitalizes output
;; prints: EGGS, BREAD, BUTTER, CARROTS.
;; prints: Eggs, bread, butter, carrots.
Note that not only is the list of values iterated over directly by format
, but the commas correctly are printed between items, not after them. A yet more complex example would be printing out a list using customary English phrasing:
(let ((template "The lucky winners were:~#[ none~; ~S~; ~S and ~S~
~:;~@{~#[~; and~] ~S~^,~}~]."))
(format nil template)
;; ⇒ "The lucky winners were: none."
(format nil template 'foo)
;; ⇒ "The lucky winners were: FOO."
(format nil template 'foo 'bar)
;; ⇒ "The lucky winners were: FOO and BAR."
(format nil template 'foo 'bar 'baz)
;; ⇒ "The lucky winners were: FOO, BAR, and BAZ."
(format nil template 'foo 'bar 'baz 'quux)
;; ⇒ "The lucky winners were: FOO, BAR, BAZ, and QUUX."
)
Whilst format
is somewhat infamous for its tendency to become opaque and hard to read, it provides a remarkably concise yet powerful syntax for a specialised and common need.[1]
References
Books
- Common Lisp HyperSpec Section 22.3 Formatted Output
- Practical Common Lisp Chapter 18. A Few FORMAT Recipes