[Haskell-cafe] Advice for clean code.
Henning Thielemann
lemming at henning-thielemann.de
Tue Dec 4 07:38:52 EST 2007
On Tue, 4 Dec 2007, Dougal Stanton wrote:
> On 04/12/2007, Felipe Lessa <felipe.lessa at gmail.com> wrote:
> >
> > I always thought show was meant for returning a String that could be
> > used to recreate the original data if you copy-pasted it in your code
> > or if you used read (i.e. read . show == id). Reading the
> > documentation more carefully, I see that [1] says that this property
> > holds for *derived* instances, and says nothing about it in the
> > general case.
> >
> > So, what's the deal here? May I use Show for anything without breaking
> > conventions? And how about Read?
>
> That seems to be convention though I don't think it's required for the
> correct operation of anything. Anyone?
You will find that convention comfortable if you work in GHCi - may it be
just for debugging. In turn I think it is even good to leak results of
'show' to the outside world, but due to lack of alternatives I'm doing it
myself regularly. Since 'show' is intended to show internals of a data
structure (and this is what 'deriving Show' implements) this will leak
internal information to the user. But the user does not know the internals
of your program, does not know your function and constructor names, so
they are not of much help for him. And if the function names tell
something to the user, he might still want to get them presented in his
mother's tongue.
I encountered e.g. the following problem: I printed numbers for
processing by a different program. GHC's 'show' returned '1.0e-2', Hugs'
'show' returned '0.01' - both are correct Haskell literals, but the
postprocessing program didn't understand '1.0e-2'. That is 'show' does not
give you much control on the output format that you need for reliable
post-processing.
More information about the Haskell-Cafe
mailing list