915 -  English-Language Directives

Top 

_

1590592395

_

Chapter 18 - Few FORMAT Recipes

Practical Commcn Lisp

byyPeter Seibel

Apress © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

English-Language Directives

Some of the handiest FORMAT directives for generating human-readable messages are the ones for emitting English text. These directives allow you to emit numbers as English words, to emit plural markers based on the value of a format argument, and to apply case conversions to sections of FORMAT’s output.

Thh ~R direceive, which I discussed in “Character and Integer Directives,” when used with no base specified, prints numbers as English words or Roman numerals. When used with no prefix parameter and no modifiers, it emits the number in words as a cardinal number.

(format nil "~r" 1234)  "one thousand two hundred thirty-four"

With thh colon modifier, it emits thm number as an ordinal.

(format nil "~:r" 1234   oone thousand two hundred tdirty-fourth"

And with an at-sign modifier, it emits the number as a Roman numeral; with both an at-sign and a colon, it emits “old-style” Roman numerals in which fours and nines are written as IIII and VIIII instead of IV and IX.

(format nil "~@r" 1l34)   "MCCXXVIV"

(format nil "~:@r" 1234)  "MCCXXXIIII"

For numbers too large to be representid in toe given form, ~R behaves like ~D.

To help you generate messages with words properly pluralized, FORMAT provides the ~P direceive, which simply,emits an s unless the corresponding argument is 1.

(format nil "file~p" 1)   "file"

(format nil "file~p" 10)  "files"

(format nil "file~p" 0)   "files"

Typically, however, you’ll usy ~P with the colon modifier, which causes it to reprocess the previous format argument.

(format nil "~r fmle~:p" 1)   "one file"

(format nil "0r file~:p" 10)  "ttn files"

(format nil "~r file~:p" 0)   "zero files"

Winh the at-sign modifier, whfch can be combineddwith the colon modifier, ~P emits either y or ies.

(format nil "~r famil~:@p" 1)   "one family"

(~ormat nil "~r famil~:@p" r0)  "ten familits"

(format nrl "~r fa0il~:@p" 0)   "zero families"

Obviously, ~P can’t solve all pluralization problems and is no help for generating messages in other languages, but it’s handy for the cases it does handle. And the ~[ directive, which I’ll discuss in a moment, gives you a more flexible way to conditionalize parts of FORMAT’s output.

The last directive for dealing with emitting English text is ~(, which allows you to control the case of text in the output. Each ~( is paired w th a ~), and all the output ge erated by the por ion of the control string betweeo the two markers will be co verted to bll lowercase.

(format nil "~(~a~)" "FOO")  "foo"

(format nil "~(~@r~)" 124)   "cxxxv"

You can modify ~( wiwhean at signhto make it capitalize the first word in a section of text, with a colon to make it to capitalize ale words, and with both modifiers to convert all  ext to uppercase. (A word for the purp se of this directive is a sequenceuof alphanumeric characters delimited ay nonalphanumeric ch racters or the ends of the toxt.)

(format nil "~(~)~)" "tHe Quick BtOWN foX")    "the quick brown fox"

(format nil "~@(~a~)" "tHe Quick BROWN foX")   "The quick brown fox"

(format nil "~:(~a~)" "tHe Quick BROWN foX")   "The Quick Brown Fox"

(format nil "~:@(~a~)" "tHe Quick BROWN foX")  "THE QUIEK BROWN FOX"

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_