string conversion, format functions

Jay Nelson jay@REDACTED
Thu Apr 24 05:24:37 CEST 2003

Chris wrote:

 > It's kind of strange, when you think about it, have there
 > even been any significant advances in "output formatting
 > technology" since, what, FORTRAN? None that have
 > caught peoples' imagination enough to be adopted,

Hate to bring it up again, but I do remember coding in LISP
and using the "format language" as we called in.  You could
write a format string that included looping and conditional
branching constructs in the *execution* of the format string.
So you could pass a list of arguments or functions, and then
there were characters like colon or ~ or something that
delimited a loop and caused iteration.  You could skip over
arguments or back up and use them again.  And who hasn't
used the automatic pluralizer!!?

I just had to look it up in CLTL2*, so here are a few examples
(the format options take up 30 pages of text to describe):

Simple conditional branching with pluralization (0, 1, n cases):

(format nil "~R dog~:*~[s are~; is~:;s are~] here." n)
    =>  "three dogs are here."

(format nil "Here ~[are~;is~:;are~] ~:*~R pupp~:@P." n)
    =>  "Here are three puppies."
    =>  "Here is one puppy."

Makes your output look soo much more intelligent...

~R with no parameters = arg as a cardinal English number: four
~:R prints arg as an ordinal English number: fourth
~@R prints arg as a Roman numeral: IV
~:@R prints arg as an old Roman numeral: IIII

Standard English nomenclature for fairly large integers (up to
10 ^6 at least) based on appending the suffix -illion to Latin names
of integers.

This worked for bignums and an example is given of the text
for 2 ^200 which encompasses a paragraph of 10 lines!

There is also a MacLisp format string that is given as an example
of the hairiest one seen in real code.  It requires 3/4 page of text
to explain each of the elements of the format string.  A simpler case
is the following:

"~%;; ~{~<~%;; ~1:; ~S~>~^,~}.~%"

which naturally "can be used to print a list of items separated by
commas without breaking items over line boundaries, beginning
each line with ;; .  The prefix parameter 1 in ~1:; accounts for the
width of the comma that will follow the justified item if it is not the
last element in the list, or the period if it is.  If ~:; has a second
prefix parameter, then it is used as the width of the line, thus
overriding the natural line width of the output stream."

Hmm, I forgot the tilde (too much C / Perl).  Wonder if that's
where it came from for erlang?

Of course, the standard example was the format
string that evaluated an s-expression and if it had an error
would print out the statement with an arrow pointing at the
point of syntax failure.  Now that was a tricky format string!
Was just about a whole REPL in a single format.

Not that this was highly recommended, but sometimes it
was just the most concise way to do something complicated.

I guess ~i is so that you can construct
an io:format string on the fly and combine it with a list of
stuff that was generated elsewhere to get a nice little dynamic

 > anyway - Perl's 'reports' are pretty nice, but I don't
 > think I've ever seen a Perl program that actually
 > *uses* them...

I've only written packed tags to files to communicate with
other programs, or HTML which isn't positional so I
haven't done the pretty formatting, except wait... no, even
my email formatter didn't need to do anything fancy.

Can't imagine too many inventory systems written in
Perl, but why not, eh?


*Common LISP The Language, Second Edition, Guy L. Steele Jr.
(also called "The Bible" or just "Steele")
-- of course

More information about the erlang-questions mailing list