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 PAA27766; Thu, 29 Apr 2004 15:38:34 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 PAA27729 for ; Thu, 29 Apr 2004 15:38:33 +0200 (MET DST) Received: from gatekeeper.elmer.external.excelhustler.com (gatekeeper.excelhustler.com [68.99.114.105]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i3TDcVYM010793 for ; Thu, 29 Apr 2004 15:38:31 +0200 Received: from chatterbox.elmer.internal.excelhustler.com (unknown [192.168.0.12]) (using TLSv1 with cipher EDH-RSA-DES-CBC3-SHA (168/168 bits)) (Client CN "chatterbox.elmer.internal.excelhustler.com", Issuer "excelhustler.com" (not verified)) by gatekeeper.elmer.external.excelhustler.com (Postfix) with ESMTP id D193EE0234; Thu, 29 Apr 2004 08:38:30 -0500 (CDT) Received: from localhost (localhost [127.0.0.1]) by chatterbox.elmer.internal.excelhustler.com (Postfix) with ESMTP id 957AB5C006; Thu, 29 Apr 2004 08:38:30 -0500 (CDT) Received: from wile.internal.excelhustler.com (wile.internal.excelhustler.com [192.168.1.34]) by chatterbox.elmer.internal.excelhustler.com (Postfix) with ESMTP id 7ED385C005; Thu, 29 Apr 2004 08:38:30 -0500 (CDT) Received: by wile.internal.excelhustler.com (Postfix, from userid 1000) id 75C061B058; Thu, 29 Apr 2004 08:38:30 -0500 (CDT) Date: Thu, 29 Apr 2004 08:38:30 -0500 From: John Goerzen To: skaller Cc: Jon Harrop , caml-list Subject: Re: [Caml-list] [ANN] The Missing Library Message-ID: <20040429133830.GE11323@excelhustler.com> References: <200404280613.19547.jdh30@cam.ac.uk> <1083141467.9537.845.camel@pelican.wigram> <200404281018.14913.jdh30@cam.ac.uk> <1083151482.9537.904.camel@pelican.wigram> <20040428151818.GC1310@excelhustler.com> <1083169714.9537.1102.camel@pelican.wigram> <20040428180243.GA4404@excelhustler.com> <1083200049.9537.1189.camel@pelican.wigram> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1083200049.9537.1189.camel@pelican.wigram> User-Agent: Mutt/1.5.5.1+cvs20040105i X-Scanned-By: clamscan at chatterbox.elmer.internal.excelhustler.com X-Miltered: at concorde by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 2004:99 ignores:01 inverted:01 implemented:01 iterator:01 bug:01 incremented:01 semantics:01 string:03 string:03 wrote:03 library:03 slightly:03 iter:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Thu, Apr 29, 2004 at 10:54:09AM +1000, skaller wrote: > > > if (!start <> 0) then print "]" > > > > > > and please don't tell me that isn't a total mess compared with > > > > Of course, your example ignores nicer features available for > > this sort of thing. > > Indeed. If I may be so bold to observe .. you've control > inverted. You're now driving the iteration. You've > got rid of the HOF List.iter and implemented your own > special one. Isn't fold_left an iterator too, just one that works with slightly different semantics? Of course, your example could be simplified to: let print_list l = let count = ref 0 in let printfunc item = print_string (if !count = 0 then "[" else ";"); print_string item; count := !count + 1 in List.iter printfunc l; if !count > 0 then print_string "]";; > The point of the code I displayed was that it > control inverts *without* getting rid of the > driver loop. > > Also .. a small bug in your translation: it prints "[]" > for an empty list. The original prints "". No. Again, my code was: let print_list l = let printfunc count item = print_string (if count = 0 then "[" else ";"); print_string item; count + 1 in if (List.fold_left printfunc 0 l) > 0 then print_string "]";; Try it. If the list is empty, printfunc is never called since there is nothing to iterate over. The return value from fold_left is zero since the count was never incremented, so the if statement evalutes to false and "]" is never called. -- John ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners