[Haskell-cafe] Re: Pretty Print, text or ++?

Benedikt Huber benjovi at gmx.net
Fri Aug 15 09:53:30 EDT 2008


Paul Keir schrieb:
 > Hi there,
 >
 > I'm writing a pretty printer using the Text.PrettyPrint library, and
 > there's a pattern I'm coming across quite often. Does anyone know 
whether,
 >
 > text (a ++ b ++ c ++ d)
 > or
 > text a <+> text b <+> text c <+> text d
 >
 > runs quicker?

Hi Paul,

 > text (a ++ b ++ c ++ d)

is the same as

 > hcat (map text [a,b,c,d])

(horizontal concatenation without seperating spaces)
while

 > text a <+> text b <+> text c <+> text d

corresponds to

 > hsep (map text [a,b,c,d])

or

 > text (unwords [a,b,c,d])

With <+>, hsep or hcat, pretty printing won't choose the best layout -
you tell the pretty printer to layout documents 'beside'.
For autolayout, see sep,cat and the paragraph-fill variants fsep and fcat.

Regarding performance: `unwords` will propably be a little faster 
(untested), but less flexible. There is no asymptotic overhead when 
using the pretty printer.

cheers,
benedikt

 >
 > Cheers,
 > Paul
 >
 >
 > ------------------------------------------------------------------------
 >
 > _______________________________________________
 > 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