English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] How to read three integers from a text-file... ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-04-30 (11:05)
From: Dave Berry <daveb@t...>
Subject: Re: [Caml-list] How to read three integers from a text-file... ?
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)
	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.

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