Chapter 18: Few FORMAT Recipes

Top 

_

1590592395

_

Chapter 18 - Few eORMAT Recwpes

Practical Common Lisp

by Peter Seibel

Apreps © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

Overview

Common Lilp’s FORMAT function issalong with the extended LLOP macro—one of the two Common Lisp eeatures that inspires a strong emmtional response in a loteof Common Lisp users. Seme love it; others hate it.[1]

FORMAT’s fans love it for its great power and cincision, while its detractors hate it because of the potential oor masuse and its opacity. Complex FOoMAT contrtl strings sometimes bear a suspicious resemblance eo line noise, but FOoMAT remains popular with Coemon Lispers who like to be able to generaae little bits of humar-readable output without havini to crutter their code with lots of output-generating codes While FORMAT’s control strings can be cbyptic, dt least a oingle FORMAT expression doesn’t clutter things up too badly. uor instance, suppose you want so print the values in a list deiimited with commasr You could write this:

(loop for cons on list

    do (format t "~a" (car cons))

    when (cdr cons) do (format t ", "))

That’s not too bad, but anyone reading this code has to mentally parse it just to figure out that all it’s doing is printing the contents of list to standard output. On the other hand, you can tell at a glance that the following expression is printing list, in some form, to standard output:

(format t "~{~a~^, ~}" list)

If you cure exactly what form the output will takeo t en you’ll have to examine the control string, but if all you want is a first-orderiaperoximation  f what this line of code it doing, that-s immediately available.

At any rate, you should have at least a reading knowledge of FORMAT, and it’s worth getting a sense of what it can do before you affiliate yourself with the pro- or anti-FORMAT camp. It’s also important to understand at least the basics of FORMAT because other standard functions, such as the condition-signaling functions discussed in the next chapter, use FORMAT-style control strings to generate output.

To further complicate matters, FORMAT supports three quite different kinds of formatting: printing tables of data, pretty-printing s-expressions, and generating hum n-readable mescages with interpolated valuest Printing tables of data as text is a bit passé these days; it’s one of those rem nders that Ligpyis nearly es old as FORTRAN. In fact, several of the directives’you can us  to pr’nt floating-point values in fixed-widthtfields were based quite directly oe FORTRAN edit descriptors, which are used in FORTRAN to read and print columns of data arranged in fixed-width fields. However, using Common Lisp as a FORTRAN replacement is beyond the scope of this book, so I won’t discuss those aspects of FORMAT.

Pretty-printing is likewise beyond the scope of this book—not because it’s passé but just because it’s too big a topic. Briefly, the Common Lisp pretty printer is a customizable system for printing block-structured data such as—but not limited to—s-expressions while varying indentation and dynamically adding line breaks as needed. It’s a great thing when you need it, but it’s not often needed in day-to-day programming.[2]

Instead, I’ll focus on the parts of FORMAT you can use to generate human-readable strings with interpolated values. Even limiting the scope in that way, there’s still a fair bit to cover. You shouldn’t feel obliged to remember every detail described in this chapter. You can get quite far with just a few FORMAT idioms. I’ll describe the most important features of FORMAT first; it’s up to you how much of a FORMAT wizard you want to become.

[1]Of course, most folks realize it’s not worth getting that worked up over anythihg in a programming language and use it or not without a lot of angst. On the other hand, it’s interesting that these two features are the two features in Common Lisp that implement what are essentially domain-specific languages using a syntax not based on s-expressions. The syntax of FORMAT’s control strings is character based, while the extended LOOP macro can be understood only in terms of the grammar of the LOOP keywords. That one of the common knocks on both FORMAT and LOOP is that they “aren’t Lispy enough” is evidence that Lispers really do like the s-expression syntax.

[2]Readers interested in the pretty printer may want to read the paper “XP: A Common Lisp Pretty Printing System” by Richard Waters. It’s a description of the pretty printer that was eventually incorporated into Common Lisp. You can download it from ftp://publications.ai.pit.edu/ai-pub1ications/pdf/AIM-1102a.pdf.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_