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 NAA21360; Tue, 30 Apr 2002 13:05:51 +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 NAA21356 for ; Tue, 30 Apr 2002 13:05:50 +0200 (MET DST) Received: from smarthost0.mail.uk.easynet.net (smarthost0.mail.uk.easynet.net [212.135.6.10]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g3UB5m501236; Tue, 30 Apr 2002 13:05:49 +0200 (MET DST) Received: from tnt-1-240.easynet.co.uk ([195.40.206.240] helo=7p8420j) by smarthost0.mail.uk.easynet.net with smtp (Exim 3.35 #1) id 172VS5-000ERX-00; Tue, 30 Apr 2002 12:05:46 +0100 Message-Id: <4.1.20020430114454.009caaf0@127.0.0.1> X-Sender: daveb/pophost.tardis.ed.ac.uk@127.0.0.1 X-Mailer: QUALCOMM Windows Eudora Pro Version 4.1 Date: Tue, 30 Apr 2002 12:07:38 +0100 To: Francois.Pottier@inria.fr, caml-list@inria.fr From: Dave Berry Subject: Re: [Caml-list] How to read three integers from a text-file... ? Cc: =?iso-8859-1?Q?Fran=E7ois?= Pottier In-Reply-To: <20020429084410.A3490@pauillac.inria.fr> References: <20020424212316.GA26318@cs.net.pl> <15557.14957.358556.545541@absurd.mimuw.edu.pl> <20020424212316.GA26318@cs.net.pl> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk At 08:44 29/04/2002, Francois Pottier wrote: >Check out Olivier Danvy's paper `Functional Unparsing': > > http://www.brics.dk/RS/98/12/ > >It describes a very nice way of programming `printf' within ML's type >system. `scanf' could be handled in a similar way. Scanf would be a little harder. For printf (actually Danvy describes sprintf), each combinator extends a string with either the next string literal or the string representation of the next argument, using string catenation. For scanf, the result type has to be a tuple. Therefore you need a function that takes an arbitrary n-tuple (the values read so far) and returns an (n+1)-tuple of those values plus the value read by the current combinator. Most implementations of ML do not provide such a function. There are extensions of ML type systems that do include this operation. I don't know whether it is included in the record calculus that underlies Objective ML (the object part of OCaml). Or how easy it would be to add. Such an operation would be useful for scanning functions in general. E.g. it could be used in a regexp library for handling \(...\) pairs. >Furthermore, this approach >yields code that is claimed to be more efficient than O'Caml's current >`printf' implementation (because the overhead of interpreting format strings >is lower). Lastly, it scales up to more expressive format directives, such as >a directive for printing a list. On the other hand, once you've converted: sprintf("%d is %s\n", i, s) into: format(int oo lit " is " oo str oo eol) i s you might just as well write: concat [int i; " is "; s; "\n"] which is shorter, and arguably easier to read, than Danvy's version. This is the approach taken in the SML Basis Library. It would be interesting to know how the performance of this approach compares with Danvy's approach. Dave. ------------------- 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