[Haskell-cafe] Re: Pure Haskell Printf

Keean Schupke k.schupke at imperial.ac.uk
Tue Nov 16 05:55:51 EST 2004


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. Remember C is typesafe, so 
the only
way they could do this was to pass the first argument (the format string) to
extract the types of the following arguments. Haskell doesn't need such 
tricks
as it is polymorphic, we can just use 'show' no matter what the type is.

Keean.

Peter Simons wrote:

>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. :-)
>
>Peter
>
>_______________________________________________
>Haskell-Cafe mailing list
>Haskell-Cafe at haskell.org
>http://www.haskell.org/mailman/listinfo/haskell-cafe
>  
>



More information about the Haskell-Cafe mailing list