caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: William Harold Newman <william.newman@airmail.net>
To: caml-list@inria.fr
Subject: [Caml-list] queasiness about Exit
Date: Thu, 15 Nov 2001 09:38:45 -0600	[thread overview]
Message-ID: <20011115093845.A26661@rootless> (raw)

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 <william.newman@airmail.net>
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


             reply	other threads:[~2001-11-15 16:04 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-11-15 15:38 William Harold Newman [this message]
2001-11-15 16:58 ` Andreas Rossberg
2001-11-15 20:41 ` John Prevost
     [not found] ` <9t0skf$d27$1@qrnik.zagroda>
2001-11-15 21:43   ` Marcin 'Qrczak' Kowalczyk
2001-11-16  8:30 ` Francois Pottier
2001-11-16 12:05 ` Lauri Alanko
2001-11-18  0:01   ` Daniel de Rauglaudre
2001-11-15 17:33 Krishnaswami, Neel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20011115093845.A26661@rootless \
    --to=william.newman@airmail.net \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).