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?
>
>
>
>Eray Ozkural wrote:
>
>On Sunday 27 April 2003 17:13, Siegfried Gonzi wrote:
>
>>quarter:: [Real] Real -> [Real]
>>quarter ls nan = sum_it 0 0.0 0 ls []
>>where
>>
>
>Ah! Why don't we have "where" in ocaml? I really miss it!
>
>Thanks,
>


Hi guys:

I diged out my old Clean manuals and tried everything starting from 
foldr, list-comprehension, function composition (f o g o g), but I did 
fail and my general solution is as follows:

==
module stat
import StdEnv

       
meanFromList:: [Real] Real -> Real
meanFromList ls nan
    # ls_nan = filter ((<) nan) ls
    # n = length(ls_nan)
    | n > 0 = sum(ls_nan)/toReal(n)
    | otherwise = nan
   
   
qMeans:: [Real] Real Int -> [Real]
qMeans [] nan q = []
qMeans ls nan q = [(meanFromList (take q ls) nan):(qMeans (drop q ls) 
nan q)]


Start =  qMeans [toReal(x) \\ x<-[1..100]] (-1.0) 2
==

The solution needs the following Clean standard functions: filter, 
length, sum, take and drop.

The overhead of take and drop is negligible.

Regards,
S. Gonzi
PS: Or if you like where:
==
qMeans:: [Real] Real Int -> [Real]
qMeans [] nan q = []
qMeans ls nan q = [(meanFromList (take q ls) nan):(qMeans (drop q ls) 
nan q)]
where
        meanFromList:: [Real] Real -> Real
        meanFromList ls nan
                # ls_nan = filter ((<) nan) ls
                # n = length(ls_nan)
                | n > 0 = sum(ls_nan)/toReal(n)
                | otherwise = nan
==

>



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