[Haskell-cafe] How to pretty print code efficiently

John Ky newhoggy at gmail.com
Fri Jul 3 21:45:47 EDT 2009


Hi,

Currently I'm pretty printing code by building arrays of strings and calling
indent.  For example:

instance JavaPrintableNamed AST.EnumeratedType where
   javaLinesNamed parentName (AST.EnumeratedType memberDefinitions) =
      [ "public enum " ++ asJavaId(parentName)
      , "{"
      ] ++ memberCodeLines ++
      [ "}"
      , ""
      ]
      where
         memberCodeLines = indent $ javaLines memberDefinitions

The indent function takes a list of strings and adds an indent to the
beginning of every line.

I can imagine this to be very inefficient as it builds many strings and
concatenates them.

In Ruby, I might do the same thing like this:

class EnumeratedType < JavaPrintableNamed
   def writeTo(writer)
      writer.print "public enum "
      writer.puts self.asJavaId
      writer.puts "{"
      writer.indent do
         self.memberDefinitions.writeTo(writer)
         writer.puts
      end

where above, the writer.indent takes care of the indent, and everything is
appended to a stream, which doesn't seem so bad in terms of efficiency.

I'm looking for a way to do something similar in Haskell.

Anyone can give me a hand?

Thanks

-John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090703/493a94e9/attachment.html


More information about the Haskell-Cafe mailing list