Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] queasiness about Exit
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: William Harold Newman <william.newman@a...>
Subject: [Caml-list] queasiness about Exit
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 =
      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
    with Exit ->

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
        (fun element -> if predicate element then raise Exit)
    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 -> .."

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:  FAQ:
To unsubscribe, mail  Archives: