From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 49E5EBBBB for ; Sun, 22 Jan 2006 17:02:28 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k0MG2RK1017512 for ; Sun, 22 Jan 2006 17:02:28 +0100 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA31900 for ; Sun, 22 Jan 2006 17:02:27 +0100 (MET) Received: from ptb-relay02.plus.net (ptb-relay02.plus.net [212.159.14.213]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id k0MG2Qf1020692 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Sun, 22 Jan 2006 17:02:27 +0100 Received: from [80.229.56.224] (helo=chetara) by ptb-relay04.plus.net with esmtp (Exim) id 1F0hfe-0007Rp-3o for caml-list@inria.fr; Sun, 22 Jan 2006 16:02:26 +0000 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. Subject: Re: [Caml-list] Pervasives or Printf module ? Date: Sun, 22 Jan 2006 15:34:04 +0000 User-Agent: KMail/1.8.2 References: <43D3A058.6030305@yahoo.fr> In-Reply-To: <43D3A058.6030305@yahoo.fr> Cc: caml-list MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline To: "Undisclosed.Recipients": ; Message-Id: <200601221534.04835.jon@ffconsultancy.com> X-Miltered: at concorde with ID 43D3AC93.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 43D3AC92.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 pervasives:01 printf:01 printf:01 sprintf:01 parentheses:01 pervasives:01 iter:01 iter:01 ocaml:01 frog:98 wrote:01 integer:01 computation:01 spaces:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 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