caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Andreas Rossberg <rossberg@ps.uni-sb.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] queasiness about Exit
Date: Thu, 15 Nov 2001 17:58:32 +0100	[thread overview]
Message-ID: <3BF3F438.E1F484C0@ps.uni-sb.de> (raw)
In-Reply-To: <20011115093845.A26661@rootless>

William Harold Newman wrote:
> 
> 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

As an aside: As you only want to "map for effect" you better use
iteration, i.e. an "iter" function.

Also note that searching actually is neither mapping nor iteration, but
a fold operation. For example, you could define exists for arrays as
follows:

	let exists p = Array.fold_left (fun b x -> b || p x) true

But it always walks the whole array.

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

Yes, I don't like that idiom either - although I use it myself
sometimes.

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

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:

	iter' : ('a -> bool) -> 'a array -> unit

Unlike the standard version of iter, the argument function returns a
boolean saying whether iteration should continue. Similarly for folds:

	fold_left' : ('a -> 'b -> 'a * bool) -> 'a -> 'b array -> 'a

With this version, you could do linear search efficiently:

	let exists p = fold_left' (fun b x -> let b' = b || p x in (b', not
b')) true

The somewhat redundant pair is a bit annoying in this particular case,
but should not really matter.

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.

Hope this helps,

	- Andreas

-- 
Andreas Rossberg, rossberg@ps.uni-sb.de

"Computer games don't affect kids; I mean if Pac Man affected us
 as kids, we would all be running around in darkened rooms, munching
 magic pills, and listening to repetitive electronic music."
 - Kristian Wilson, Nintendo Inc.
-------------------
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:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-11-15 15:38 William Harold Newman
2001-11-15 16:58 ` Andreas Rossberg [this message]
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=3BF3F438.E1F484C0@ps.uni-sb.de \
    --to=rossberg@ps.uni-sb.de \
    --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).