Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] pretty printers and format and matrices
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Markus Mottl <mottl@m...>
Subject: Re: [Caml-list] pretty printers and format and matrices
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