Michael Marte wrote:

> > Michael Marte wrote:
> >
> > > I have a simple problem but it seems to be quite involved: I want to print
> > > floating point numbers with a given number of decimal points.
> > >
> > > First, I found out that both ghc and hugs do not consider the precision
> > > argument to showPrec.
> >
> > What precision argument?  There's a precedence argument, but the precision
> > is news to me...
>
> The library report defines:
>
> class  Show a  where
>     showsPrec :: Int -> a -> ShowS
>     show      :: a -> String
>     showList  :: [a] -> ShowS
>
>     showsPrec _ x s   = show x ++ s
>     show x        = showsPrec 0 x ""
>
> I always thought that the Int argument to showsPrec is the precision.
> So what is it good for? The library report does not explain it.

It's used to indicate the precedence when you print with operators to avoid
parenthesis.  You can e.g. make something print as

    a * b + c * d

rather than

    (a * b) + (c * d)

using the precedence.

--

        -- Lennart




Reply via email to