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 OAA23210; Thu, 15 Jul 2004 14:37:50 +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 OAA22779 for ; Thu, 15 Jul 2004 14:37:49 +0200 (MET DST) Received: from ptb-relay02.plus.net (ptb-relay02.plus.net [212.159.14.213]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i6FCbmSH020239 for ; Thu, 15 Jul 2004 14:37:48 +0200 Received: from [80.229.56.224] (helo=chetara) by ptb-relay02.plus.net with esmtp (Exim) id 1Bl5Uh-000ABG-Ou for caml-list@inria.fr; Thu, 15 Jul 2004 12:37:47 +0000 From: Jon Harrop To: caml-list@inria.fr Subject: Re: [Caml-list] assertions or exceptions? Date: Thu, 15 Jul 2004 13:35:22 +0100 User-Agent: KMail/1.6.2 References: <7f8e92aa04071501035091cbe9@mail.gmail.com> In-Reply-To: <7f8e92aa04071501035091cbe9@mail.gmail.com> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200407151335.23097.postmaster@jdh30.plus.com> X-Miltered: at concorde with ID 40F67A9C.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; postmaster:99 caml-list:01 api:01 recursions:01 higher-order:01 avoids:01 posts:01 sanity:01 ocaml:01 ocaml:01 assertions:01 assertions:01 polymorphic:01 polymorphic:01 simpler:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > There are other libraries, notably Java API and .NET class library, > that use exceptions liberaly... OCaml and the programming styles when used in OCaml are quite different to those of Java and most .NET languages (F# being a notable exception ;-). In OCaml, you often write in a functional style, when performing many similar operations this naturally lends itself to recursive functions. When running, a deeply recursed function may suddenly find a shortcut to the correct answer. Exceptions provide an ideal means to escape such deep recursions. The alternative, e.g. using polymorphic options, are typically considerably less efficient, either in terms of speed of execution or in terms of extra source code. This is particularly true in the context of polymorphic higher-order functions, as the following example (hopefully!) shows. > But why is a boolean better than an exception? Because it makes code > simpler. Convert the following code (which finds the product of a bunch of integers) into an equivalent which avoids exceptions but retains the efficiency of being able to bail earlier, upon encountering a zero: exception Zero let prod fold l = try fold (fun a e -> if e=0 then raise Zero else a*e) 1 l with Zero -> 0 > Exceptions can be left uncaught and this is a potential cause > for a combinatorial explosion of the number of possible "execution > paths"... I would say it was poor style to allow the possible number of execution paths to explode. > 1. Is my impression that OCaml standard library is abusing exceptions > correct? No, it is based upon valid arguments for imperative languages which are not applicable here. > 2. Is it safe to assume that exceptions complicate functional > programs as much as they complicate imperative ones? As the above example shows, exceptions can be used to simplify code. > 3. Is it possible to avoid using exceptions and read a text file > line-by-line until EOF? Yes (see other people's posts). > 4. When do you use assertions and when do you use exceptions? I use assertions to perform sanity checks. I use exceptions in two separate sitations: firstly to handle exceptional circumstances, secondly to provide an escape route from computations. Cheers, Jon. ------------------- 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