Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Martin Jambon <martin_jambon@emailuser.net>
To: Nicolas George <nicolas.george@ens.fr>
Cc: Caml mailing list <caml-list@inria.fr>, Don Syme <dsyme@microsoft.com>
Subject: Re: [Caml-list] Checking for eof
Date: Mon, 27 Dec 2004 12:23:11 -0800 (PST)	[thread overview]
Message-ID: <Pine.LNX.4.44.0412271203010.1556-100000@localhost> (raw)
In-Reply-To: <20041226130904.GA24805@clipper.ens.fr>

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





  reply	other threads:[~2004-12-27 20:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-26  1:12 romildo
2004-12-26  1:58 ` [Caml-list] " briand
2004-12-26 13:09   ` Nicolas George
2004-12-27 20:23     ` Martin Jambon [this message]
2004-12-26 15:25 Don Syme
2004-12-27  0:14 ` skaller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.44.0412271203010.1556-100000@localhost \
    --to=martin_jambon@emailuser.net \
    --cc=caml-list@inria.fr \
    --cc=dsyme@microsoft.com \
    --cc=nicolas.george@ens.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox