caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Checking for eof
@ 2004-12-26  1:12 romildo
  2004-12-26  1:58 ` [Caml-list] " briand
  0 siblings, 1 reply; 4+ messages in thread
From: romildo @ 2004-12-26  1:12 UTC (permalink / raw)
  To: caml-list

Hi.

I am missing a way of checking end of file
for an input channel. How would I write
it?

  val eof : in_channel -> bool

Romildo


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Caml-list] Checking for eof
  2004-12-26  1:12 Checking for eof romildo
@ 2004-12-26  1:58 ` briand
  2004-12-26 13:09   ` Nicolas George
  0 siblings, 1 reply; 4+ messages in thread
From: briand @ 2004-12-26  1:58 UTC (permalink / raw)
  To: romildo; +Cc: caml-list


Youa are actually looking for an eof exception.

Here is a code segment which demonstrates this nicely and won't blow
up the stack.

let readfile chan =
  let rec loop rlst =
    let line, eof = 
      try
        (input_line chan), false
      with
        | End_of_file -> "", true
    in
      if not eof then
	(
          loop (line :: rlst);
	)
      else
        List.rev rlst
  in
    loop []
;;

Brian


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Checking for eof
  2004-12-26  1:58 ` [Caml-list] " briand
@ 2004-12-26 13:09   ` Nicolas George
  2004-12-27 20:23     ` Martin Jambon
  0 siblings, 1 reply; 4+ messages in thread
From: Nicolas George @ 2004-12-26 13:09 UTC (permalink / raw)
  To: Caml mailing list

[-- Attachment #1: Type: text/plain, Size: 886 bytes --]

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.

[-- Attachment #2: Type: application/pgp-signature, Size: 185 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Checking for eof
  2004-12-26 13:09   ` Nicolas George
@ 2004-12-27 20:23     ` Martin Jambon
  0 siblings, 0 replies; 4+ messages in thread
From: Martin Jambon @ 2004-12-27 20:23 UTC (permalink / raw)
  To: Nicolas George; +Cc: Caml mailing list, Don Syme

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





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-12-27 20:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-26  1:12 Checking for eof romildo
2004-12-26  1:58 ` [Caml-list] " briand
2004-12-26 13:09   ` Nicolas George
2004-12-27 20:23     ` Martin Jambon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).