caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Marcin 'Qrczak' Kowalczyk" <qrczak@knm.org.pl>
To: caml-list@inria.fr
Subject: Re: [Caml-list] queasiness about Exit
Date: Thu, 15 Nov 2001 21:43:35 +0000 (UTC)	[thread overview]
Message-ID: <slrn9v8do7.k04.qrczak@qrnik.zagroda> (raw)
In-Reply-To: <9t0skf$d27$1@qrnik.zagroda>

Thu, 15 Nov 2001 17:58:32 +0100, Andreas Rossberg <rossberg@ps.uni-sb.de> pisze:

> 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

An alternative is

fold_right' : ('a -> (unit -> 'b) -> 'b) -> 'a array -> (unit -> 'b) -> 'b

which can also replace iter (with 'b = unit).

When fold in either direction could be used, fold_left is usually
beter if the folding function is strict, and fold_right if it's lazy.

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

let exists p xs = fold_right' (fun x bf -> p x || bf ()) xs (fun () -> false)

Wrapping "init" (shouldn't it be called "last" here?) in a closure is
unnecessary for this case but it's important e.g. for concatenation
of lazy lists.

-- 
 __("<  Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^
QRCZAK

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


  parent reply	other threads:[~2001-11-15 21:43 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
2001-11-15 20:41 ` John Prevost
     [not found] ` <9t0skf$d27$1@qrnik.zagroda>
2001-11-15 21:43   ` Marcin 'Qrczak' Kowalczyk [this message]
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=slrn9v8do7.k04.qrczak@qrnik.zagroda \
    --to=qrczak@knm.org.pl \
    --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).