Chapter 11. Printing Text with the format Function

Even in this modern era of programming, it’s extremely important to be able to manipulate text, and Common Lisp has some of the fanciest text-printing functions available. Whether you need to manipulate XML, HTML, Linux configuration files, or any other data in a textual format, Lisp will make your work easy.

The most important advanced text printing function in Common Lisp is the format function, which is the subject of this chapter.

Anatomy of the format Function

Here is an example of the format function in use:

> (format t "Add onion rings for only ˜$ dollars more!" 1.5)
Add onion rings for only 1.50 dollars more!
NIL

Let’s take a look at what each part of this function means.

image with no caption

The Destination Parameter

The first parameter to the format function is the destination parameter, which tells format where to send the text it generates. Here are its possible values:

nil

Don’t print anything; just return the value as a string.

t

Print the value to the console. In this case, the function just returns nil as a value (as in our example).

stream

Write the data to an output stream (covered in Chapter 12).

In the following example, we set the first parameter to nil so it simply returns the value as a string:

> (princ (reverse
        (format nil "Add onion rings for only ˜$ dollars more!" 1.5)))
 !erom srallod 05.1 ylno rof sgnir noino ddA
 "!erom srallod 05.1 ylno rof sgnir noino ddA"

The resulting string value ("Add onion rings for only 1.50 dollars more!") is passed to the reverse function, and then that reversed string is printed to the screen with the princ command .

In this example, the REPL will also print the value of the entered expression, along with the information output by the princ command. This is why you see the value displayed a second time . For the remainder of this chapter, the examples will omit these values printed by the REPL, and show only the information explicitly printed by our code.

The Control String Parameter

The second parameter to the format function is a control string, which controls the text formatting. The format function’s power lies in the control string. In our current example, the control string is "Add onion rings for only ˜$ dollars more!".

By default, the text in this string is simply printed as output. However, you can place control sequences into this string to affect the format of the output, as described in the remainder of this chapter. Our current example contains the control sequence ˜$, which indicates a monetary floating-point value. Every control sequence recognized by the format function begins with the tilde (˜) character.

Value Parameters

The format parameters following the control string contain values, or the actual data to be displayed and formatted. As you’ll see, the control string interacts with these parameters and controls their formatting.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.187.233