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 QAA32171; Fri, 26 Apr 2002 16:55:14 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA32147 for caml-list@pauillac.inria.fr; Fri, 26 Apr 2002 16:55:13 +0200 (MET DST) 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 WAA21078 for ; Thu, 25 Apr 2002 22:47:22 +0200 (MET DST) Received: from laurelin.dementia.org ([208.167.88.73]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g3PKlJT26773; Thu, 25 Apr 2002 22:47:19 +0200 (MET DST) Received: (from jprevost@localhost) by laurelin.dementia.org (8.11.6/8.11.6) id g3PKqNP53895; Thu, 25 Apr 2002 16:52:23 -0400 (EDT) (envelope-from visigoth@cs.cmu.edu) X-Authentication-Warning: laurelin.dementia.org: jprevost set sender to visigoth@cs.cmu.edu using -f To: Chris Hecker Cc: =?iso-8859-1?q?J=E9r=F4me?= Marant , caml-list@inria.fr, Daniel de Rauglaudre Subject: Re: [Caml-list] How to read three integers from a text-file... ? References: <20020425111905.GA31096@kiefer.ai.univie.ac.at> <15557.14957.358556.545541@absurd.mimuw.edu.pl> <20020424212316.GA26318@cs.net.pl> <20020425105559.C392@verdot.inria.fr> <20020425111905.GA31096@kiefer.ai.univie.ac.at> <4.3.2.7.2.20020425105510.02d0a600@mail.d6.com> From: John Prevost Date: 25 Apr 2002 16:52:22 -0400 In-Reply-To: <4.3.2.7.2.20020425105510.02d0a600@mail.d6.com> Message-ID: <86lmbbtsah.fsf@laurelin.dementia.org> User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.1 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk >>>>> "ch" == Chris Hecker writes: ch> As Markus says, the Printf.printf doesn't work in this case ch> anyway right now, since it happens completely at compile time, ch> I believe. Actually, this is not the case, though printf formats are limited in what you can do with them. (i.e. you may substitute, but not combine.) As an example: # let fmt x = (x : ('a,'b,'c) format) let a = fmt "First: %d, Second: %d\n" let b = fmt "[1, %d; 2, %d]\n" let f fmt = Printf.printf fmt 10 10 let _ = f a let _ = f b ;; First: 10, Second: 10 [1, 10; 2, 10] val fmt : ('a, 'b, 'c) format -> ('a, 'b, 'c) format = val a : (int -> int -> unit, out_channel, unit) format = val b : (int -> int -> unit, out_channel, unit) format = val f : (int -> int -> 'a, out_channel, unit) format -> 'a = The definition of fmt is just to provide a quick way to force a string into the context of a format type--otherwise, the cimpiler would assign the type string to that value. So it is possible to do some small things with these formats. A more interesting strategy is to use formatting combinators: let id x = x let ( ** ) f g x = f (g x) let str k s x = k (s ^ x) let int k s x = k (s ^ string_of_int x) let lit p x k s = p k s x let lis p k s x = match x with | [] -> k (s ^ "[]") | _ -> let rec loop xs k s = match xs with | [x] -> p (fun s -> k (s ^ "]")) s x | x::xs -> p (fun s -> loop xs k (s ^ ";")) s x in loop x k (s ^ "[") let format p = p id "" Which lets you do things like: # format (int ** lit int 3) 5;; - : string = "53" # format (int ** lit str " -> " ** lis int) 5 [1;2;3;4;5];; - : string = "5 -> [1;2;3;4;5]" Of course, these combinators can be extended for other ways of producing results, and you can add new formats later. So, it's more extensible than the current printf is. Quite nice, really. John. ------------------- 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