From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id XAA30425; Fri, 17 Sep 2004 23:32:28 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id XAA31313 for ; Fri, 17 Sep 2004 23:32:27 +0200 (MET DST) Received: from yquem.inria.fr (yquem.inria.fr [128.93.8.37]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id i8HLW6uS030775; Fri, 17 Sep 2004 23:32:07 +0200 Received: by yquem.inria.fr (Postfix, from userid 24253) id 58FDBBC43; Fri, 17 Sep 2004 23:32:06 +0200 (CEST) Date: Fri, 17 Sep 2004 23:32:06 +0200 To: Micha Cc: caml-list@inria.fr, Erik de Castro Lopo Subject: Re: [Caml-list] Passing printf format strings to functions Message-ID: <20040917213206.GA13234@yquem.inria.fr> References: <20040915075547.54c0bef6.ocaml-erikd@mega-nerd.com> <200409150008.34253.micha-1@fantasymail.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200409150008.34253.micha-1@fantasymail.de> User-Agent: Mutt/1.3.28i From: weis@yquem.inria.fr (Pierre Weis) X-Miltered: at concorde with ID 414B57D6.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 passing:01 printf:01 weis:01 pierre:01 weis:01 2004:99 fmt:01 endline:01 printf:01 fmt:01 val:01 pervasives:01 val:01 renaming:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > Hi, > > Am Tuesday 14 September 2004 23:55 schrieb Erik de Castro Lopo: > > Hi all, > > let rec print_string_pairs (fmt:string) = > > function > > [] -> print_endline "" > > > > | a :: b -> > > > > Printf.printf fmt (fst a) (snd a) ; > > print_string_pairs fmt b > > ;; > > > I think the fmt variable has not to be of type string but of some > format type. Exactly. Hence you just have to remove the wrong type constraint (fmt:string) and simply write fmt, to get it working beautifully: val print_string_pairs : ('a -> 'b -> 'c, out_channel, unit) format -> ('a * 'b) list -> unit > there exists a conversion function in Pervasives: > val format_of_string : ('a, 'b, 'c, 'd) format4 -> ('a, 'b, 'c, 'd) format4 > (*format_of_string s returns a format string read from the string literal > s.*) In this case I doubt you ever need to use this function: the type-checker will do the job automatically for you! For instance: # print_string_pairs "(%s, %s)";; - : (string * string) list -> unit = Or even: # print_string_pairs "(%i, %i)";; - : (int * int) list -> unit = At this point, you should consider renaming your function, given its highly polymorphic nature, it shoould be promoted to print_pairs ... Best regards, -- Pierre Weis INRIA, Projet Cristal, http://pauillac.inria.fr/~weis ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners