[Haskell-cafe] Re: Pure Haskell Printf

Peter Simons simons at cryp.to
Tue Nov 16 05:42:15 EST 2004

Henning Thielemann writes:

 >> One advantage is that you need to type fewer characters.

 > I know memory is expensive, that's why only the last two
 > digits of year numbers are stored. :-]

I understand what you're getting at -- and I find it
annoying, too, when people sacrifice robustness for comfort.

I'm not sure, though, whether this is the case here, because
vsnprintf in Haskell still is type-safe. You'll get more
complicated error messages, the memory footprint might be
worse, but it still _is_ robust code.

Perhaps it really is a matter of personal taste.

Just for the sake of seeing the point from all possible
perspectives, I could think of another reason why you might
need a function like that: If you want to provide
printf-style variable substitutions to the user, say in a
config file. People are _used_ to this mechanism, and many
programs I know offer this feature to customize text
templates, etc. It can't hurt to have a function that does
the parse job and returns the result for you.

Although, if you think this through, you'll soon arrive at
the conclusion that for this particular use-case hard-coded
place-holders (like %s, %d, etc.) are not that useful. You'd
like to be able to (easily!) extend the function, to offer a
more general variable substitution, like sh(1) does. So that
you could write

  Dear ${customer},

  are you interested in making ${phantasy-number} fast?

and pass a function (String -> a) to vsnprintf which does
the lookup. I'm not sure how having different types of
values in the dictionary plays into this, though.

 > You can save even more characters:

 > msg  = verb "i = " . shows 12 . verb "\tj = " $ "test"

Right! One more reason to use ShowS-style. :-)


More information about the Haskell-Cafe mailing list