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 PAA26946; Thu, 15 Jul 2004 15:45:44 +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 PAA26721 for ; Thu, 15 Jul 2004 15:45:43 +0200 (MET DST) Received: from mproxy.gmail.com (mproxy.gmail.com [216.239.56.252]) by concorde.inria.fr (8.12.10/8.12.10) with SMTP id i6FDjfSH029604 for ; Thu, 15 Jul 2004 15:45:42 +0200 Received: by mproxy.gmail.com with SMTP id w29so1167017cwb for ; Thu, 15 Jul 2004 06:45:34 -0700 (PDT) Received: by 10.11.118.39 with SMTP id q39mr58524cwc; Thu, 15 Jul 2004 06:45:34 -0700 (PDT) Message-ID: <7f8e92aa04071506453ef7345d@mail.gmail.com> Date: Thu, 15 Jul 2004 16:45:34 +0300 From: Radu Grigore To: caml-list@inria.fr Subject: Re: [Caml-list] assertions or exceptions? In-Reply-To: <200407151335.23097.postmaster@jdh30.plus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: <7f8e92aa04071501035091cbe9@mail.gmail.com> <200407151335.23097.postmaster@jdh30.plus.com> X-Miltered: at concorde with ID 40F68A85.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 2004:99 postmaster:99 recursions:01 avoids:01 exploding:99 posts:01 sanity:01 invariants:01 assertion:01 ocaml:01 workaround:01 assertions:01 assertions:01 rec:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Thu, 15 Jul 2004 13:35:22 +0100, Jon Harrop wrote: > [...] 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. This is an interesting idea. I'll try to spot such situations while coding! > 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;; All I can do is this: let rec prod_fold a l = match a, l with | 0, _ -> 0 | a, [] -> a | a, h :: t -> prod_fold (a * h) t Differences between these are: 1. Yours makes an exception visible to other functions that do not use it 2. Yours is 80 non-ws characters long, mine is 71 characters long; so they are comparable. 3. Mine is less flexible: it does not allow plugging in a fold function. However I don't see where such a thing would be useful. An example? 4. I assert that mine is easier to understand. (at least for me :) ) One reason is that it doesn't use exceptions: only a simple recursive function. 5. I see no reason why performance would be significantly different. > > 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. You can use goto and keep the number of execution paths from exploding too. This doesn't mean that it is not a dangerous tool and that you should be careful while using it. The same applies to exceptions. > As the above example shows, exceptions can be used to simplify code. I'm not convinced. > > 3. Is it possible to avoid using exceptions and read a text file > > line-by-line until EOF? > > Yes (see other people's posts). Those solutions are: 1. use another library 2. provide your own workaround (by redesigning the interface) I fail to see how this vindicates the design of the standard library. > > 4. When do you use assertions and when do you use exceptions? > > I use assertions to perform sanity checks. You mean internal invariants? That is, including postconditions but not preconditions? > I use exceptions in two separate > sitations: firstly to handle exceptional circumstances, Tautologies do not contain a lot of information ;-) When is something exceptional? When it shouldn't happen but you know what to do if it does happen? If you remove the "but you know what to do.." part then you are better of using an assertion. > secondly to provide an escape route from computations. As I said, this is an interesting idea but I really need to see it in practice in order to understand it. regards, radu ------------------- 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