[Haskell-cafe] Re: Pure Haskell Printf

Matthew Walton matthew at alledora.co.uk
Tue Nov 16 07:44:41 EST 2004


Ben Rudiak-Gould wrote:
> Keean Schupke wrote:
> 
>  > At the risk of getting off topic... the reason 'C' has printf is because
>  > it is not polymorphic. Printf is a hack to allow different types to be
>  > printed out, such that they did not need printInt, printFloat etc.
> 
> Many language have printf-like functions despite not satisfying this
> criterion. Perl, Python, and Common Lisp are the three that come to mind.
> 
> I think the reason they have it is that it's useful in general to be able
> to visually separate the string template from the expressions being
> printed. Even (name++", your age is"++age++".") is pretty punctuation-heavy
> for a template. (Plus, it has a bug in it, which would be much easier to
> see in the printf syntax.)

Perl, as I see it, has printf for two reasons. The first is because 
sometimes

printf "something%dsomething else", $number;

is nicer to deal with than

print "something${number}something else";

but I think primarily because printf lets you specify the number 
formatting and other such things, which Perl's variable interpolation 
system won't. Things like

printf "%02d", $number;

Are invaluable at times. That, as I see it, is the value of printf-like 
functions. It's certainly preferable (no matter how painful to the parts 
of us which like nice clean pure code) to C++'s rather unpleasant 
iostreams system of throwing random objects down a stream to change the 
number formatting pattern. Ugh.



More information about the Haskell-Cafe mailing list