Note From Practical Common Lisp
Common Lisp’s FORMAT function is—along with the extended LOOP macro—one of the two Common Lisp features that inspires a strong emotional response in a lot of Common Lisp users. Some love it; others hate it.
The first argument to FORMAT, the destination for the output, can be T, NIL, a stream, or a string with a fill pointer. T is shorthand for the stream *STANDARD-OUTPUT*, while NIL causes FORMAT to generate its output to a string, which it then returns.3 If the destination is a stream,the output is written to the stream. And if the destination is a string with a fill pointer, the formatted output is added to the end of the string and the fill pointer is adjusted appropriately.Except when the destination is NIL and it returns a string, FORMAT returns NIL.Common Lisp’s FORMAT function is—along with the extended LOOP macro—one of the two Common Lisp features that inspires a strong emotional response in a lot of Common Lisp users. Some love it; others hate it.
All directives start with a tilde (~) and end with a single character that identifies the directive.You can write the character in either upper- or lowercase. Some directives take prefix parameters, which are written immediately following the tilde, separated by commas, and used to control things such as how many digits to print after the decimal point when printing a floating-point number.
The value of a prefix parameter can also be derived from the format arguments in two ways: A prefix parameter of v causes FORMAT to consume one format argument and use its value for the prefix parameter. And a prefix parameter of # will be evaluated as the number of remaining format arguments.The most general-purpose directive is ~A, which consumes one format argument of any type and outputs it in aesthetic (human-readable) form.The other two most frequently used directives are ~%, which emits a newline, and ~&, which emits a fresh line.The ~R directive, which I discussed in “Character and Integer Directives,” when used with no base specified, prints numbers as English words or Roman numerals.Another FORMAT directive that you’ve seen already, in passing, is the iteration directive ~{. This directive tells FORMAT to iterate over the elements of a list or over the implicit list of the format arguments.
No comments:
Post a Comment