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 KAA16639; Fri, 25 Apr 2003 10:24:06 +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 KAA17734 for ; Fri, 25 Apr 2003 10:24:05 +0200 (MET DST) Received: from mailhost.tni.fr (firewall.tni.fr [195.25.255.61]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h3P8O4H17411 for ; Fri, 25 Apr 2003 10:24:04 +0200 (MET DST) Received: from groscool.tni.fr ([195.25.255.1]) by mailhost.tni.fr (Netscape Messaging Server 3.62) with SMTP id 3682; Fri, 25 Apr 2003 10:23:45 +0200 Received: from 192.168.7.60 by groscool.tni.fr (InterScan E-Mail VirusWall NT); Fri, 25 Apr 2003 10:23:45 +0200 (Paris, Madrid (heure d'été)) Message-ID: <3EA8EF7E.59EFC26C@tni.fr> Date: Fri, 25 Apr 2003 10:19:10 +0200 From: "sebastien FURIC" X-Mailer: Mozilla 4.78 [fr] (Windows NT 5.0; U) X-Accept-Language: fr MIME-Version: 1.0 To: Siegfried Gonzi CC: OCaml Mailing list Subject: Re: [Caml-list] Easy solution in OCaml? References: <3EA8D012.8040700@stud.uni-graz.at> Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Spam: no; 0.00; furic:01 tni-valiosys:01 caml-list:01 siegfried:01 functors:01 interresting:01 functor:01 val:01 quarterly:99 struct:01 failwith:01 0.0:01 30.0:99 bool:01 ocaml:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hi Siegfried, I wrote the following Ocaml program that performs the task using functors. The "interresting stuff" is in module Means: It computes the means given rules you define into the NUMERICTYPE module you pass to the functor. As you can see, there is no need to change the code even if you change your numeric type or the meaning of is_nan, zero, one, + and /. module type NUMERICTYPE = sig type t val is_nan: t -> bool val zero: t val one: t val ( + ): t -> t -> t val ( / ): t -> t -> t end module type MEANS = sig type t val quarterly_means: t list -> t * t * t * t end module Means(NumericType : NUMERICTYPE) : (MEANS with type t = NumericType.t) = struct open NumericType type t = NumericType.t let mean xs = let rec mean' sum n = function | [] -> sum / n | x :: xs when is_nan x -> mean' sum n xs | x :: xs -> mean' (sum + x) (n + one) xs in mean' zero zero xs let quarterly_means = function | [a; b; c; d; e; f; g; h; i; j; k; l] -> mean [a; b; c], mean [d; e; f], mean [g; h; i], mean [j; k; l] | _ -> failwith "quarterly_means: 12 months expected" end (* tests *) module Integer = struct type t = int let is_nan i = i = -1 let zero = 0 let one = 1 let ( + ) = ( + ) let ( / ) = ( / ) end ;; module Float = struct type t = float let is_nan f = classify_float f = FP_nan let zero = 0.0 let one = 1.0 let ( + ) = ( +. ) let ( / ) = ( /. ) end ;; module MeansInteger = Means(Integer);; MeansInteger.quarterly_means [1; 2; 4; -1; 45; 56; 45; 56; 8; 10; 30; 5];; module MeansFloat = Means(Float);; MeansFloat.quarterly_means [1.0; 2.0; 4.0; 0.0 /. 0.0; 45.0; 56.0; 45.0; 56.0; 8.0; 10.0; 30.0; 5.0];; Cheers, Sébastien. ------------------- 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