caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Markus Mottl <mottl@miss.wu-wien.ac.at>
To: Chris Hecker <checker@d6.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] pretty printers and format and matrices
Date: Tue, 12 Jun 2001 21:03:42 +0200	[thread overview]
Message-ID: <20010612210342.A662@miss.wu-wien.ac.at> (raw)
In-Reply-To: <4.3.2.7.2.20010612111351.029b8720@shell16.ba.best.com>; from checker@d6.com on Tue, Jun 12, 2001 at 11:18:13 -0700

On Tue, 12 Jun 2001, Chris Hecker wrote:
> Format has come up on the list a couple times, so I figured I'd ask
> a question I gave up on trying to solve a while back.

Once one gets used to it, it's really fun so don't give up...

> I finally gave up and ended up with this (open Bigarray first):
> 
> let print2d a =
>   for i = 0 to Array2.dim1 a - 1 do
>     for j = 0 to Array2.dim2 a - 1 do
>       Format.printf "%f\t" a.{i,j}
>     done;
>     Format.printf "\n";
>   done

The reason is obviously the use of "\n": this is a "real" newline, which
disregards indentation levels. The pretty printer will still assume that
it hasn't broken lines if you use this, which makes further output look
really strange.

> But this doesn't play nice with other printers like it should.  I tried
> a few different attemps at putting the boxes in, but none worked.
> I also read the FAQ, but couldn't quite apply its info to this problem.
> Is there a "right" way to do this, or is the Format module not set up
> for vertical printing?

You'll have to use "@\n" instead or also "Format.force_newline ()"
if you want to force a newline without confusing indentation levels.

The Format-module is very powerful! In most cases it will suffice if
you use hov-boxes (the default with "@[") + indentation annotations as
required. Try this for instance:

---------------------------------------------------------------------------
open Bigarray
open Format

let print2d ppf a =
  fprintf ppf "@[<2>[|";
  for i = 0 to Array2.dim1 a - 1 do
    pp_force_newline ppf ();
    for j = 0 to Array2.dim2 a - 1 do
      fprintf ppf "%f;\t" a.{i,j}
    done
  done;
  fprintf ppf "@]@\n|]"

let _ =
  let ar = Array2.of_array float32 c_layout [|[|1.; 2.|]; [|3.; 4.|]|] in
  printf "%a@\n" print2d ar
---------------------------------------------------------------------------

Regards,
Markus Mottl

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


  reply	other threads:[~2001-06-12 19:03 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-12 18:18 Chris Hecker
2001-06-12 19:03 ` Markus Mottl [this message]
2001-06-12 21:01   ` Chris Hecker
2001-06-13 12:16     ` Hendrik Tews
2001-06-14 19:37       ` Pierre Weis
2001-06-14 19:58         ` Patrick M Doane
2001-06-14 20:36           ` Pierre Weis
2001-06-14 21:35         ` Frank Atanassow
2001-06-15 12:43           ` Pierre Weis
2001-06-15 12:34         ` Hendrik Tews
2001-06-15 17:32           ` Pierre Weis
2001-06-14  6:46     ` Pierre Weis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20010612210342.A662@miss.wu-wien.ac.at \
    --to=mottl@miss.wu-wien.ac.at \
    --cc=caml-list@inria.fr \
    --cc=checker@d6.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).