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 CAA22396; Thu, 26 Apr 2001 02:40:05 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id CAA22956 for ; Thu, 26 Apr 2001 02:40:05 +0200 (MET DST) Received: from fledge.watson.org ([204.156.12.50]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f3Q0e0P24775 for ; Thu, 26 Apr 2001 02:40:04 +0200 (MET DST) Received: from localhost (patrick@localhost) by fledge.watson.org (8.11.3/8.11.3) with SMTP id f3Q0chJ44610; Wed, 25 Apr 2001 20:38:43 -0400 (EDT) (envelope-from patrick@watson.org) Date: Wed, 25 Apr 2001 20:38:43 -0400 (EDT) From: Patrick M Doane To: Chris Hecker cc: caml-list@inria.fr Subject: Re: [Caml-list] two unrelated questions In-Reply-To: <200104252108.OAA02055@smtp2-cm.mail.eni.net> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Wed, 25 Apr 2001, Chris Hecker wrote: > > 1. What is the right "functional pattern" for early-outing on success > while using an iter/map/fold type function? Say I'm using iter to > search for something in an opaque datastructure. Should I throw > an exception to get out, or is that bad style? I guess this > question only makes sense for iter, since map/fold produce results > that you theoretically want to preserve. So, the question is > really, given an iter-style interface to a datastructure (one that > takes an ('a -> unit)), how do you tell it to stop iterating? I > guess if the function was ('a -> bool) you could do it that way, > but most iters aren't ((List|Array|Hashtbl).iter, for example). > Is throwing an exception the best bet? > One way to think about exceptions is to treat them simply as control flow expressions. For example, the 'break' statement is used in C/C++ to early-out of iteration constructs. This translates naturally to exceptions: exception Break try List.iter (fun .. -> ... raise Break) l with Break -> () We can even get labelled breaks like Java: exception Break of string try List.iter (fun ... -> try List.iter (fun ... -> if .. then raise (Break "inner") else raise (Break "outer") ) list1 with Break "inner" -> () ) list2 with Break "outer" -> () I personally don't think this is bad style but others may have a different opinion. It also seems that the overhead for a try block in Caml is relatively low, but I've never looked too much at the compiled code to see what it is doing. Patrick ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr