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 OAA23403; Tue, 30 Apr 2002 14:20:22 +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 OAA23398 for ; Tue, 30 Apr 2002 14:20:21 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g3UCKF504924; Tue, 30 Apr 2002 14:20:15 +0200 (MET DST) Received: (from fpottier@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id OAA23383; Tue, 30 Apr 2002 14:20:15 +0200 (MET DST) Date: Tue, 30 Apr 2002 14:20:15 +0200 From: Francois Pottier To: Dave Berry Cc: caml-list@inria.fr Subject: Re: [Caml-list] How to read three integers from a text-file... ? Message-ID: <20020430142015.B21691@pauillac.inria.fr> Reply-To: Francois.Pottier@inria.fr References: <20020424212316.GA26318@cs.net.pl> <15557.14957.358556.545541@absurd.mimuw.edu.pl> <20020424212316.GA26318@cs.net.pl> <20020429084410.A3490@pauillac.inria.fr> <4.1.20020430114454.009caaf0@127.0.0.1> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Mailer: Mutt 1.0i In-Reply-To: <4.1.20020430114454.009caaf0@127.0.0.1>; from daveb@tardis.ed.ac.uk on Tue, Apr 30, 2002 at 12:07:38PM +0100 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hi Dave, On Tue, Apr 30, 2002 at 12:07:38PM +0100, Dave Berry wrote: > > Scanf would be a little harder. [...] For scanf, the result type has to be a > tuple. No, it doesn't, if you program in CPS style. Instead of returning a tuple, return a function that expects a continuation, and applies it successively to several values. You obtain the same effect, except in that case, ``tuple concatenation'' becomes typable. A similar trick was used by Didier Rémy in ``Typing record concatenation for free'' back in 1993. The function that concatenates CPS-encoded tuples is as follows: let (++) tuple1 tuple2 k = tuple2 (tuple1 k) A simple ``tuple'' that contains one integer (read from standard input when the tuple is queried) is let int k = k (int_of_string (input_line stdin)) You can then read, say, three integers from standard input and compute something out of them in the following way: (int ++ int ++ int) (fun x y z -> x + y * z) As far as I can tell, this approach scales up to format specifiers other than `int'. > Such an operation would be useful for scanning functions in general. E.g. > it could be used in a regexp library for handling \(...\) pairs. Yes, it would be very nice to use this approach for regexps. Unfortunately, Danvy's encoding relies on the fact that the format specification is *not* a string. So, it would be possible to devise something along these lines, but the good old regexp syntax would probably have to be dropped. -- François Pottier Francois.Pottier@inria.fr http://pauillac.inria.fr/~fpottier/ ------------------- 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