From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id WAA11366; Thu, 15 Nov 2001 22:43:09 +0100 (MET) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA11602 for ; Thu, 15 Nov 2001 22:43:08 +0100 (MET) Received: from mail.mimuw.edu.pl (paf87.warszawa.sdi.tpnet.pl [217.96.225.87]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id fAFLh1j17974 for ; Thu, 15 Nov 2001 22:43:03 +0100 (MET) Received: (from news@localhost) by mail.mimuw.edu.pl (PLD/8.9.3) id WAA20807 for caml-list@inria.fr; Thu, 15 Nov 2001 22:43:38 +0100 X-Authentication-Warning: qrnik.zagroda: news set sender to Marcin 'Qrczak' Kowalczyk using -f From: "Marcin 'Qrczak' Kowalczyk" Subject: Re: [Caml-list] queasiness about Exit Date: Thu, 15 Nov 2001 21:43:35 +0000 (UTC) Organization: Klub Nieszkodliwych =?iso-8859-2?Q?Manjak=F3w?= Message-ID: References: <20011115093845.A26661@rootless> <9t0skf$d27$1@qrnik.zagroda> X-Trace: qrnik.zagroda 1005860615 20485 192.168.0.1 (15 Nov 2001 21:43:35 GMT) X-Complaints-To: abuse@localhost NNTP-Posting-Date: Thu, 15 Nov 2001 21:43:35 +0000 (UTC) User-Agent: slrn/0.9.7.2 (Linux) To: caml-list@inria.fr Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Thu, 15 Nov 2001 17:58:32 +0100, Andreas Rossberg 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