From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 9802DBC2F for ; Sat, 4 Dec 2004 15:12:27 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id iB4ECRDj013418 for ; Sat, 4 Dec 2004 15:12:27 +0100 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id PAA05931 for ; Sat, 4 Dec 2004 15:12:26 +0100 (MET) Received: from smtp2.wanadoo.fr (smtp2.wanadoo.fr [193.252.22.29]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id iB4ECQ20014468 for ; Sat, 4 Dec 2004 15:12:26 +0100 Received: from me-wanadoo.net (unknown [127.0.0.1]) by mwinf0204.wanadoo.fr (SMTP Server) with SMTP id 65FA51C00241; Sat, 4 Dec 2004 15:12:26 +0100 (CET) Received: from nono (ARouen-106-1-32-91.w80-14.abo.wanadoo.fr [80.14.22.91]) by mwinf0204.wanadoo.fr (SMTP Server) with SMTP id EE1301C00235; Sat, 4 Dec 2004 15:12:25 +0100 (CET) Message-ID: <003601c4da0b$774ac220$0100a8c0@mshome.net> From: =?iso-8859-1?Q?Fr=E9d=E9ric_Gava?= To: "sejourne_kevin" Cc: References: <9E571848-430E-11D9-AF66-000A95CDFBE4@cs.unc.edu> <281F644B-43EF-11D9-BA22-000D9345235C@inria.fr> <6FBBD2B0-44FC-11D9-AF66-000A95CDFBE4@cs.unc.edu> <1102062166.666.25.camel@localhost> <001501c4d9f1$1b1c4600$0100a8c0@mshome.net> <41B1BAD4.2000004@yahoo.fr> Subject: Re: [Caml-list] Missing a function Date: Sat, 4 Dec 2004 15:13:44 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 8bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 X-Miltered: at concorde with ID 41B1C5CB.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 41B1C5CA.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; gava:01 gava:01 caml-list:01 comparaison:01 stdlib:01 ocaml:01 hashtbl:01 functor:01 val:01 cheers:01 ...:98 ...:98 structures:01 int:01 int:01 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.0 X-Spam-Level: > > I have done a comparaison on three data structures of the stdlib of OCaml (I > > do parallel implementation of this data structure) and I find that the > > module Map does not contain any function for counting the number of elements > > of the data structure: > > Set, cardinal: t -> int > > Hashtbl, lenght : ('a,'b) t -> int > > Map, you have to do "let length the_map = fold (fun _ _ i -> i+1) > > the_map 0 " > > And Set and Hastbl have also fold... > > Is there an explanation (or is it just a missing ;-) ) ? > > There is a 'fold' function in functor Make. > val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b > so : > let cardinal e = fold (fun _ _ x -> succ x) e 0;; Hi, > > Map, you have to do "let length the_map = fold (fun _ _ i -> i+1) > let cardinal e = fold (fun _ _ x -> succ x) e 0;; Ok. That is the same things (except the name) that I have written in my mail... but this method is not very efficient because you apply many time a function without using its arguments. Map are also implemented as balanced tree (like sets) and therefore, it is easy to add a function of "cardinal". Cheers, Frédéric Gava