caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] queasiness about Exit
@ 2001-11-15 15:38 William Harold Newman
  2001-11-15 16:58 ` Andreas Rossberg
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: William Harold Newman @ 2001-11-15 15:38 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re: [Caml-list] queasiness about Exit
@ 2001-11-15 17:33 Krishnaswami, Neel
  0 siblings, 0 replies; 8+ messages in thread
From: Krishnaswami, Neel @ 2001-11-15 17:33 UTC (permalink / raw)
  To: caml-list

Andreas Rossberg [mailto:rossberg@ps.uni-sb.de] wrote:
> 
> For real mapping (as opposed to iteration) terminating early makes
> little sense. For iteration and folds it does. It is not too difficult
> to provide variations of iter and folds for your data structures that
> allow early breaks. For example, consider:
>
> BTW, this is one of the few problems where lazy evaluation 
> can play out its strengths: in a lazy language you do not need
> variations of iteration with early termination - you will always
> "terminate early" automatically.

I posted a similar question to comp.lang.functional a few months
ago, and Dan Wang showed me how I could use monads to make a
regular fold do that. 

I actually wanted to thread a state through a fold, but IIRC the 
technique will generalize to any monad (such as the exception
or continuation monad). Google reveals that the thread is at:

http://groups.google.com/groups?threadm=slrn9ig78d.gpl.neelk%40brick.cswv.co
m

--
Neel Krishnaswami
neelk@cswcasa.com
-------------------
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


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2001-11-18  0:01 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-15 15:38 [Caml-list] queasiness about Exit William Harold Newman
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

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).