From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id DAAD1BC48 for ; Sat, 9 Apr 2005 21:43:57 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j39Jhvuo029280 for ; Sat, 9 Apr 2005 21:43:57 +0200 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 VAA13742 for ; Sat, 9 Apr 2005 21:43:57 +0200 (MET DST) Received: from stag.seas.upenn.edu (stag.seas.upenn.edu [158.130.70.79]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j39JhttF029271 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Sat, 9 Apr 2005 21:43:56 +0200 Received: from [192.168.2.101] (dhcp0642.grt.resnet.group.upenn.edu [165.123.143.14]) by stag.seas.upenn.edu (8.13.3/8.12.10) with ESMTP id j39JhtoC009568 for ; Sat, 9 Apr 2005 15:43:55 -0400 Mime-Version: 1.0 (Apple Message framework v619.2) Content-Transfer-Encoding: 7bit Message-Id: <1daf59ba39e27121b56c47fda1b80d5e@cis.upenn.edu> Content-Type: text/plain; charset=US-ASCII; format=flowed To: caml-list@inria.fr From: Aaron Bohannon Subject: Printf and "%a" Date: Sat, 9 Apr 2005 15:43:54 -0400 X-Mailer: Apple Mail (2.619.2) X-Miltered: at nez-perce with ID 4258307D.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 4258307B.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; upenn:01 printf:01 printf:01 sprintf:01 sprintf:01 fprintf:01 usefulness:01 kprintf:01 kprintf:01 upenn:01 ...:98 strings:01 interaction:01 interaction:01 argument:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: The behavior of the "%a" format string in the Printf module does not seem to match the documentation. # Printf.printf "%a" ;; - : (out_channel -> '_a -> unit) -> '_a -> unit = # Printf.sprintf "%a" ;; - : (unit -> '_a -> string) -> '_a -> string = The first type exactly matches the one that would be expected based upon reading the documentation. The second type surprised me because the only documentation for sprintf is: "Same as Printf.fprintf, but instead of printing on an output channel, return a string containing the result of formatting the arguments." On an intuitive level, I can understand that it makes some sense to require a user-defined printer that returns a string in the case of sprintf. However, the need for the function to first take a unit argument is not intuitive at all to me, and it seems to hinder the usefulness of the mechanism because it is not likely that one would have already defined a printer function that has that type and can simply be plugged in. So I guess one will generally have to write: let s = Printf.sprintf "...%a..." (fun () -> thing_to_string) thing ;; Then there's kprintf: # fun k -> Printf.kprintf k "%a" ;; - : (string -> 'a) -> (unit -> 'b -> string) -> 'b -> 'a = It appears to follow the behavior of sprintf, but I read a comment in the newsgroup archives that indicated there was more complexity with the interaction of kprintf and "%a" format strings. Does this interaction ever change the type of the user-defined printer that is expected by kprintf? Again, the documentation misleadingly silent about "%a" patterns used with kprintf. -- Aaron Bohannon http://www.cis.upenn.edu/~bohannon/