From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id BAA18552; Sat, 7 Jun 2003 01:10:21 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 BAA18700 for ; Sat, 7 Jun 2003 01:10:20 +0200 (MET DST) Received: from post.it.helsinki.fi (post.it.helsinki.fi [128.214.205.24]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h56NAJH16905 for ; Sat, 7 Jun 2003 01:10:19 +0200 (MET DST) Received: from la.iki.fi (root@kruuna.helsinki.fi [128.214.205.14]) by post.it.helsinki.fi (8.12.9/8.12.2-SPAMmers-sod-off) with ESMTP id h56N93Qo025141; Sat, 7 Jun 2003 02:09:03 +0300 (EEST) Received: from la by la.iki.fi with local (Exim 3.36 #1 (Debian)) id 19OQKQ-00044j-00; Sat, 07 Jun 2003 02:08:58 +0300 Date: Sat, 7 Jun 2003 02:08:57 +0300 From: Lauri Alanko To: caml-list@inria.fr Subject: Re: [Caml-list] Why do input* and readdir throw End_of_file ... annoying! Message-ID: <20030606230856.GC5736@la.iki.fi> References: <32CAF7EC-9849-11D7-BAD2-000393863F70@exomi.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.3i X-Spam: no; 0.00; lauri:01 alanko:01 caml-list:01 readdir:01 recursion:01 beginners:01 accum:01 199912:01 ocaml:01 caml:01 handler:01 rec:01 syntax:02 06,:02 exception:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Fri, Jun 06, 2003 at 01:49:55PM -0500, Brian Hurt wrote: > The problem with try/with is that it's way to easy to break tail recursion > using try/with. About every other week someone comes to the Ocaml > beginners list where they are doing something like: > > let rec read_all_lines chan accum = > try > let line = input_line chan in > read_all_lines chan (line :: accum) > with > End_of_file -> List.rev accum This is a good argument for the alternative exception handling syntax proposed by in "Exceptional syntax", JFP 11(4):395-410, . They suggest a form try x <= e1 in e2 unless E => e3 which acts like "let x = e1 in e2" when e1 causes no exceptions, but otherwise goes directly to the handler and skips e2 entirely. So the function above could be written: let rec read_all_lines chan accum = try line <= input_line chan in read_all_lines chan (line :: accum) unless End_of_file => List.rev accum It looks almost the same, but here the scope of the handler does _not_ extend over the recursive call, only the binding Actually, as pointed out by the article, this thing has been suggested on caml-list before: . Lauri Alanko la@iki.fi ------------------- 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