Samiksha Jaiswal (Editor)

Format (Common Lisp)

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

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_.

Contents

Example

An example of a C printf call is the following:

Using Common Lisp, this is equivalent to:

Another example would be to print every element of list delimited with commas, which can be done using the ~{, ~^ and ~} directives:

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:

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.

Books

  • Common Lisp HyperSpec Section 22.3 Formatted Output
  • Practical Common Lisp Chapter 18. A Few FORMAT Recipes
  • References

    Format (Common Lisp) Wikipedia


    Similar Topics