From: "sebastien FURIC" <sebastien.furic@tni-valiosys.com>
To: Siegfried Gonzi <siegfried.gonzi@stud.uni-graz.at>
Cc: OCaml Mailing list <caml-list@inria.fr>
Subject: Re: [Caml-list] Easy solution in OCaml?
Date: Fri, 25 Apr 2003 10:19:10 +0200 [thread overview]
Message-ID: <3EA8EF7E.59EFC26C@tni.fr> (raw)
In-Reply-To: <3EA8D012.8040700@stud.uni-graz.at>
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
next prev parent reply other threads:[~2003-04-25 8:24 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-04-25 6:05 Siegfried Gonzi
2003-04-25 8:19 ` sebastien FURIC [this message]
2003-04-25 15:46 ` Brian Hurt
2003-04-25 16:34 ` Ville-Pertti Keinonen
2003-04-26 13:45 ` Siegfried Gonzi
2003-04-26 21:51 ` Brian Hurt
2003-04-27 15:01 ` Siegfried Gonzi
2003-04-28 15:43 ` Brian Hurt
2003-04-29 5:46 ` John Max Skaller
2003-04-27 16:33 ` [Caml-list] Re: IEEE-754 (was: Easy solution in OCaml?) Christophe TROESTLER
2003-04-25 16:59 ` [Caml-list] Easy solution in OCaml? Markus Mottl
2003-04-26 6:25 ` Siegfried Gonzi
2003-04-27 14:13 ` Siegfried Gonzi
2003-04-27 16:54 ` Eray Ozkural
2003-04-28 5:00 ` Siegfried Gonzi
2003-04-28 17:45 ` malc
2003-04-28 18:16 ` Shivkumar Chandrasekaran
[not found] <20030427164326.34082.qmail@web41211.mail.yahoo.com>
2003-04-28 12:05 ` Siegfried Gonzi
2003-04-28 13:54 ` Noel Welsh
2003-04-28 14:22 ` David Brown
2003-04-28 14:38 ` sebastien FURIC
2003-04-28 18:14 ` Eray Ozkural
2003-05-03 14:37 ` John Max Skaller
2003-05-03 16:57 ` Eray Ozkural
2003-04-28 15:27 isaac gouy
2003-04-28 16:38 ` brogoff
2003-04-28 17:13 ` isaac gouy
2003-04-28 17:48 ` Brian Hurt
2003-04-28 17:50 ` brogoff
2003-04-28 18:31 ` Eray Ozkural
2003-04-29 6:46 ` Siegfried Gonzi
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=3EA8EF7E.59EFC26C@tni.fr \
--to=sebastien.furic@tni-valiosys.com \
--cc=caml-list@inria.fr \
--cc=siegfried.gonzi@stud.uni-graz.at \
/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