[Haskell-cafe] Best way to build strings?

Bernard Pope bjpop at cs.mu.OZ.AU
Thu Jul 21 04:01:01 EDT 2005


On Thu, 2005-07-21 at 09:24 +0200, Tomasz Zielonka wrote:
> On Thu, Jul 21, 2005 at 04:55:15PM +1000, Bernard Pope wrote:
> > On Wed, 2005-07-20 at 17:06 +0100, Andy Gimblett wrote:
> > >     show (Prefix l p) = "(" ++ l ++ "->" ++ show p ++ ")"
> > >     show (External p q) = "(" ++ show p ++ " [] " ++ show q ++ ")"
> > > 
> > > but to me the extensive use of ++ is not particularly readable.
> > 
> > It is also inefficient because append has complexity proportional to the
> > length of its left argument. That's why the Prelude defines:
> > 
> >    type ShowS = String -> String
> > 
> > and functions like showsPrec, shows, showChar
> 
> It's not that bad in this case, because ++ is right-associative.

You are right, in this case it is not too bad.

I meant that there are potential efficiency problems with this style of
generating strings, which ShowS and the pretty printers address.

Cheers,
Bernie.



More information about the Haskell-Cafe mailing list