Hi all,<br><br>Thanks everyone for the help. The HughesPJ module works well for me.<br><br>Cheers,<br><br>-John<br><br><div class="gmail_quote">On Mon, Jul 6, 2009 at 3:49 AM, Chris Eidhof <span dir="ltr"><<a href="mailto:chris@eidhof.nl">chris@eidhof.nl</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div></div><div class="h5">On 4 jul 2009, at 05:13, Alexander Dunlap wrote:<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Fri, Jul 3, 2009 at 6:45 PM, John Ky<<a href="mailto:newhoggy@gmail.com" target="_blank">newhoggy@gmail.com</a>> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br>
<br>
Currently I'm pretty printing code by building arrays of strings and calling<br>
indent. For example:<br>
<br>
instance JavaPrintableNamed AST.EnumeratedType where<br>
javaLinesNamed parentName (AST.EnumeratedType memberDefinitions) =<br>
[ "public enum " ++ asJavaId(parentName)<br>
, "{"<br>
] ++ memberCodeLines ++<br>
[ "}"<br>
, ""<br>
]<br>
where<br>
memberCodeLines = indent $ javaLines memberDefinitions<br>
<br>
The indent function takes a list of strings and adds an indent to the<br>
beginning of every line.<br>
<br>
I can imagine this to be very inefficient as it builds many strings and<br>
concatenates them.<br>
<br>
In Ruby, I might do the same thing like this:<br>
<br>
class EnumeratedType < JavaPrintableNamed<br>
def writeTo(writer)<br>
writer.print "public enum "<br>
writer.puts self.asJavaId<br>
writer.puts "{"<br>
writer.indent do<br>
self.memberDefinitions.writeTo(writer)<br>
writer.puts<br>
end<br>
<br>
where above, the writer.indent takes care of the indent, and everything is<br>
appended to a stream, which doesn't seem so bad in terms of efficiency.<br>
<br>
I'm looking for a way to do something similar in Haskell.<br>
<br>
Anyone can give me a hand?<br>
<br>
Thanks<br>
<br>
-John<br>
<br>
<br>
_______________________________________________<br>
</blockquote>
<br>
You may want to investigate the standard module<br>
Text.PrettyPrint.HughesPJ, which contains a number of (I assume fairly<br>
efficient) combinators for pretty printing.<br>
</blockquote>
<br></div></div>
I second that. Also, there is uulib which has a pretty printing module that's quite similar:<br>
<br>
<a href="http://hackage.haskell.org/packages/archive/uulib/0.9.10/doc/html/UU-PPrint.html" target="_blank">http://hackage.haskell.org/packages/archive/uulib/0.9.10/doc/html/UU-PPrint.html</a><br>
<br>
I think both packages are based on the paper "The Design of a Pretty-printing Library" which can be found at <a href="http://www.cs.chalmers.se/%7Erjmh/Papers/pretty.ps" target="_blank">http://www.cs.chalmers.se/~rjmh/Papers/pretty.ps</a><br>
<br>
Not only do they provide abstractions for things like indentation, concatenation in different forms, etc., but they also are more efficient than a naive implementation using lists.<br>
<br>
-chris<br><font color="#888888">
<br>
-chris<br>
</font></blockquote></div><br>