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

Paul,
Something tells me you might want to look at `concat':
concat :: [[a]] -> [a]
/jve
2008/8/15 Paul Keir
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?
Cheers, Paul
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- /jve

Thanks,
So you're recommending:
text (concat [a,b,c,d,e])
Might this not transform my pretty printing into ugly printing; when longer strings are used?
Paul
-----Original Message-----
From: sw17ch@gmail.com on behalf of John Van Enk
Sent: Fri 15/08/2008 14:31
To: Paul Keir
Cc: haskell-cafe@haskell.org
Subject: Re: [Haskell-cafe] Pretty Print, text or ++?
Paul,
Something tells me you might want to look at `concat':
concat :: [[a]] -> [a]
/jve
2008/8/15 Paul Keir
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?
Cheers, Paul
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- /jve

Paul,
I'm sorry, I ignored the "PrettyPrint" part and latched onto the "faster"
part. You definitely don't want concat. I was looking at run-time. :)
/jve
On Fri, Aug 15, 2008 at 9:35 AM, Paul Keir
Thanks,
So you're recommending:
text (concat [a,b,c,d,e])
Might this not transform my pretty printing into ugly printing; when longer strings are used?
Paul
-----Original Message----- From: sw17ch@gmail.com on behalf of John Van Enk Sent: Fri 15/08/2008 14:31 To: Paul Keir Cc: haskell-cafe@haskell.org Subject: Re: [Haskell-cafe] Pretty Print, text or ++?
Paul,
Something tells me you might want to look at `concat':
concat :: [[a]] -> [a]
/jve
2008/8/15 Paul Keir
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?
Cheers, Paul
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- /jve
-- /jve

"Paul Keir"
text (a ++ b ++ c ++ d)
The above is going to be ugly-printed onto a single line, whilst this:
text a <+> text b <+> text c <+> text d
has a chance to be pretty-printed onto several lines, if each component is individually long. It doesn't really matter which expression is faster, if they do different things. The first solution fails to use the pretty-printing API to any significant advantage, whilst the second is more in the spirit of the thing. Regards, Malcolm

Paul Keir wrote:
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?
Don't worry about speed. Write it as: hsep[text a, text b, text c, text d] and you can easily change hsep to sep or fsep, if the lines get too long. (And maybe you have some other docs that may be part of the list that are not constructed using "text".) HTH Christian

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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Awesome! Thanks to you all. I'll start with hsep[map a, b, c, d] and then I can try changing hsep for other things. Paul -----Original Message----- From: Benedikt Huber [mailto:benjovi@gmx.net] Sent: Fri 15/08/2008 14:53 To: Paul Keir Cc: haskell-cafe@haskell.org Subject: Re: Pretty Print, text or ++? 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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (5)
-
Benedikt Huber
-
Christian Maeder
-
John Van Enk
-
Malcolm Wallace
-
Paul Keir