Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Francois Pottier <francois.pottier@i...>
Subject: Re: [Caml-list] How to read three integers from a text-file... ?

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