Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Easy solution in OCaml?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Siegfried Gonzi <siegfried.gonzi@s...>
Subject: Re: [Caml-list] Easy solution in OCaml?
>
>
>
>Markus Mottl wrote:
>
>>
>
>Or in OCaml, if you know how to do it elegantly and reasonably efficiently:
>
>  let coll (len, sum as acc) n = if n >= 0 then len + 1, sum + n else acc
>
>  let qmeans =
>    let rec loop acc = function
>      | a :: b :: c :: t ->
>          let len, sum = coll (coll (coll (0, 0) a) b) c in
>          loop ((if len = 0 then 0.0 else float sum /. float len) :: acc) t
>      | [] -> List.rev acc
>      | _ -> failwith "qmeans: illegal list" in
>    loop []
>
As comparison I post my Clean version (it is more general or could be 
made general, lets say for a list with 24 hours). But I am still not 
contended, because why isn't it possible to use more elegant 
"functional-constructs" which lead to short, easy to read and easy to 
comprehend solutions (see also my post on comp.lang.functional):

==
module stat
import StdEnv


quarter:: [Real] Real -> [Real]
quarter ls nan = sum_it 0 0.0 0 ls []
where
	sum_it:: Int Real Int [Real] [Real] -> [Real]
	sum_it counter sum n [] erg = reverse erg
	sum_it counter sum n [h:t] erg
		| (counter == 2) 
			| n > 0 = sum_it 0 0.0 0 t [(sum/toReal(n)):erg]
			        = sum_it 0 0.0 0 t [nan:erg]
		| h > nan  = sum_it (counter+1) (sum+h) (n+1) t erg
		| otherwise = sum_it (counter+1) sum n t erg
		
Start = quarter [1.0,2.0,-1.0,3.4,3.4,-1.0,-1.0,2.3,3.4,-1.0,-1.0,-1.0] (-1.0)
==



Regards,
S. Gonzi


-------------------
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