[m-users.] [Beginner] How to pretty print a list of lists of strings

Julien Fischer jfischer at opturion.com
Fri Oct 14 13:43:51 AEDT 2022


On Fri, 14 Oct 2022, Razetime wrote:

> I have a list [["abc","bcd"],["acb","cbd"]] and I would like to
> prettyprint it to an IO stream.
>
> I looked into the pretty_print module but it seems to be for
> generating documentation,

It isn't for generating documentation. I guess the point of confusion
is that it refers to docs, but in the context of pretty_printer module
these are references to the doc/0 type defined by that module.
That type can be used to format arbitrary Mercury terms (and more
generally constuct documents potentially containing arbitrary Mercury
terms).

To pretty-print the above term to stdout using the pretty_printer
module, you could just do:

      L = [["abc","bcd"],["acb","cbd"]],
      pretty_printer.write_doc_formatted(L, !IO)

> and format from the io and strings module seems to be for simpler
> types. How would I go about doing this? I want to know if there's
> something i can use from the stdlib, similar to show in haskell.

I haven't used Haskell in quite some time, so I can remember the
exact semantics of show, however ...

In the string module, the function string/1 will convert an arbitrary
term into a string representation.

The io module has the print family of functions (io.print/3, io.print/4
io.print_line/3 etc.) and the write family of function (io.write/3,
io.write/4, io.write_line/3 etc.).  The distinction between the two
is that the print family are supposed to be human readable, whereas
the output of the write family are supposed to valid Mercury syntax.

Julien.


More information about the users mailing list