From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 6532DBB81 for ; Mon, 27 Dec 2004 21:23:37 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id iBRKNacg017170 for ; Mon, 27 Dec 2004 21:23:36 +0100 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 VAA07842 for ; Mon, 27 Dec 2004 21:23:35 +0100 (MET) Received: from out2.smtp.messagingengine.com (out2.smtp.messagingengine.com [66.111.4.26]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id iBRKNZaf017167 for ; Mon, 27 Dec 2004 21:23:35 +0100 Received: from frontend3.messagingengine.com (frontend3.internal [10.202.2.152]) by frontend1.messagingengine.com (Postfix) with ESMTP id 5F20CC47965; Mon, 27 Dec 2004 15:23:33 -0500 (EST) X-Sasl-enc: 2SWZVsDhtfKrkbVHAr3Mcw 1104179011 Received: from [172.16.112.115] (burnham.ljcrf.edu [192.231.106.2]) by frontend3.messagingengine.com (Postfix) with ESMTP id D4FC52876F; Mon, 27 Dec 2004 15:23:30 -0500 (EST) Date: Mon, 27 Dec 2004 12:23:11 -0800 (PST) From: Martin Jambon X-X-Sender: martin@localhost To: Nicolas George Cc: Caml mailing list , Don Syme Subject: Re: [Caml-list] Checking for eof In-Reply-To: <20041226130904.GA24805@clipper.ens.fr> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=iso-8859-1 Content-Transfer-Encoding: 8BIT X-Miltered: at concorde with ID 41D06F48.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 41D06F47.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 wrote:01 nivose:01 ocaml:01 syntax:01 unpack:01 syntax:01 stand-alone:01 semantics:01 rec:01 rec:01 compiler:01 ...:98 ...:98 exceptions:01 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.0 X-Spam-Level: On Sun, 26 Dec 2004, Nicolas George wrote: > Le sextidi 6 nivôse, an CCXIII, briand@aracnet.com a écrit : > > try > > (input_line chan), false > > with > > | End_of_file -> "", true > > I would have written that > > try > Some (input_line chan) > with > | End_of_file -> None > > but the idea is the same. I find it is an irritating limitation of OCaml > syntax to have to pack and then unpack all local values in order to uncatch > exceptions. Something like > > try > let line = input_line chan in > untry > loop (line :: rlst) > with > | End_of_file -> List.rev rlst > > This syntax is somewhat awkward: untry is neither a third member of the > try...with structure, because it must be inside the flow of let...in > declaration, nor a stand-alone statement, because it must not be allowed > anywhere outside try...with. > > On the contrary, as far as I can see, the semantics is quite simple. Instead of this (try too large): let readfile chan = let rec loop rlst = try let line = input_line chan in loop (line :: rlst) with End_of_file -> List.rev rlst in loop [] We can write that: let readfile chan = let rec loop rlst = (try let line = input_line chan in fun () -> loop (line :: rlst) with End_of_file -> fun () -> List.rev rlst) () in loop [] And it seems to be handled efficiently by the compiler (confirmation?). (and for the desperate, it is not difficult to write a Camlp4 syntax extension which does this :-) Martin -- Martin Jambon, PhD Researcher in Structural Bioinformatics since the 20th Century The Burnham Institute http://www.burnham.org San Diego, California