Version franēaise
Home     About     Download     Resources     Contact us    
Browse thread
Request for comments: Printf list conversion
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jean-Christophe Filliatre <filliatr@l...>
Subject: Re: [Caml-list] Request for comments: Printf list conversion

Hello,

Alessandro Baretta writes:
 > I would like to transform the following rather vague idea into a
 > formal feature  
 > request. Before doing this, I would like to know what others think about it.
 > 
 > I continually come across the need to pretty-print via Printf a list of 
 > values--for example, let's say an int list. 

I recently submitted a similar wish on the bug tracking system, which
is slightly more general than your proposal:

  http://caml.inria.fr/mantis/view.php?id=3882

Meanwhile, I'm using a rather different solution than yours, that is a
custom function

  val print_list : (formatter -> 'a -> unit) -> formatter -> 'a list -> unit

that I use with %a, e.g.

  fprintf fmt "this is the list %a" (print_list pp_print_int) [1;2;3]

I  actually use a  more general  version of  print_list that  allows a
separator  to  be  specified  (to  be  printed  in  between  the  list
elements):

  val print_list : 
    ?sep:(formatter -> unit -> unit) -> 
    (formatter -> 'a -> unit) -> formatter -> 'a list -> unit

which comes with a handy list of predefined separators:

  val space : formatter -> unit -> unit
  val newline : formatter -> unit -> unit
  val comma : formatter -> unit -> unit
  val semi : formatter -> unit -> unit
  val nothing : formatter -> unit -> unit
  etc.

Hope this helps,
-- 
Jean-Christophe Filliātre (http://www.lri.fr/~filliatr)