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 RAA05781; Thu, 15 Nov 2001 17:04:49 +0100 (MET) 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 RAA05495 for ; Thu, 15 Nov 2001 17:04:48 +0100 (MET) Received: from mx1.airmail.net (mx1.airmail.net [209.196.77.98]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id fAFG4cj09957 for ; Thu, 15 Nov 2001 17:04:47 +0100 (MET) Received: from covert.black-ring.iadfw.net ([209.196.123.142]) by mx1.airmail.net with smtp (Exim 3.16 #10) id 164P0I-0009UH-00 for caml-list@inria.fr; Thu, 15 Nov 2001 10:04:38 -0600 Received: from rootless.localdomain from [207.136.45.4] by covert.black-ring.iadfw.net (/\##/\ Smail3.1.30.16 #30.55) with esmtp for sender: id ; Thu, 15 Nov 2001 10:05:58 -0600 (CST) Received: (from newman@localhost) by rootless.localdomain (8.10.1/8.10.1) id fAFFckV16095; Thu, 15 Nov 2001 09:38:46 -0600 (CST) Date: Thu, 15 Nov 2001 09:38:45 -0600 From: William Harold Newman To: caml-list@inria.fr Subject: [Caml-list] queasiness about Exit Message-ID: <20011115093845.A26661@rootless> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0.1i Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk When I'm searching for something in an array, the idiom seems to be to raise Exit when I find it. E.g. let is_critical critical_outputs results = try for i = 0 to Array.length results - 1 do let r = results.(i).loc in for j = 0 to Array.length critical_outputs - 1 do if critical_outputs.(j).loc = r then raise Exit done done; false with Exit -> true What if I'm searching for something in a complex data structure, using a mapping function where, in STL or in a more object-oriented system, I'd use an iterator? I could do something like let exists_in_complex_datastructure predicate complex_data_structure try map_for_effect_on_complex_data_structure (fun element -> if predicate element then raise Exit) complex_data_structure; false with Exit -> true But I'm having trouble convincing myself that this won't get me into trouble with obscure bugs later as things get complicated and "predicate" or "map_for_effect_on_complex_data_structure" might themselves be implemented in terms of "try .. raise Exit .. with Exit -> .." constructs. I've mostly programmed in Common Lisp for the last few years, and I'm accustomed to nonlocal exits with lexical scoping, so I've never had to think about this. Is there some nice nesting reason why this turns out not to be a problem? Or do careful OCaml programmers declare a new exception type for every nonlocal exit when opaque mappings or predicates are involved? Or am I missing the point somehow and asking the wrong question? (Of course, if I'm just searching for something in a complex data structure, probably the right way is to implement exists_in_complex_data_structure or a better search function as part of the interface to the data structure, instead of trying to improvise one as part of the map-over-all-elements function. In fact, I'm really not so much concerned with pure searching as with other operations which have a possibility of terminating early.) (And by the way, thanks for the answers about narrowing objects!) -- William Harold Newman PGP key fingerprint 85 CE 1C BA 79 8D 51 8C B9 25 FB EE E0 C3 E5 7C ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr