Version française
Home     About     Download     Resources     Contact us    
Browse thread
Pervasives or Printf module ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] Pervasives or Printf module ?
On Sunday 22 January 2006 15:10, sejourne_kevin wrote:
> As the usage of:
>            Printf.sprintf "_%d" x
> and
>            "_"^(string_of_int x)

That can be written without parentheses and the style guide advises the use of 
spaces around operators (i.e. "^"):

  "_" ^ string_of_int x

> is the same, what is the better solution?
>
> Do you have an opinion for the one or the second?

I tend to avoid Printf and stick with Pervasives where possible.

You can do some nice things with printf, like exploit currying:

# List.iter (Printf.printf "%d ") [1;2;3];;
1 2 3 - : unit = ()

but it is easy to trip up:

# List.iter (Printf.printf " %d") [1;2;3];;
 123- : unit = ()

Note: the former prints a space after every integer whereas the latter prints 
only a single space at the beginning. To get the latter to act as the former 
does, you must beta expand (I think that is the correct technical term) to 
postpone the computation of printf " ":

# List.iter (fun n -> Printf.printf " %d" n) [1;2;3];;
 1 2 3- : unit = ()

If efficiency is a concern then I would expect printf to be faster because it 
should avoid string concatenation. I haven't tested that though...

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists