On 4 jul 2009, at 05:13, Alexander Dunlap wrote:

On Fri, Jul 3, 2009 at 6:45 PM, John Ky<newho...@gmail.com> wrote:
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


_______________________________________________

You may want to investigate the standard module
Text.PrettyPrint.HughesPJ, which contains a number of (I assume fairly
efficient) combinators for pretty printing.

I second that. Also, there is uulib which has a pretty printing module that's quite similar:

http://hackage.haskell.org/packages/archive/uulib/0.9.10/doc/html/UU-PPrint.html

I think both packages are based on the paper "The Design of a Pretty- printing Library" which can be found at http://www.cs.chalmers.se/~rjmh/Papers/pretty.ps

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.

-chris

-chris
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to