[Haskell-cafe] Help to create a function to calculate a n element
moving average ??
ok at cs.otago.ac.nz
Mon Sep 27 21:40:29 EDT 2010
On 27/09/2010, at 5:20 AM, rgowka1 wrote:
> Type signature would be Int -> [Double] -> [(Double,Double)]
> Any thoughts or ideas on how to calculate a n-element moving average
> of a list of Doubles?
> Let's say [1..10]::[Double]
> what is the function to calculate the average of the 3 elements?
> [(1,0),(2,0),(3,2),(4,3)....] :: [(Double,Double)]
> moving_average3 (xs0 @ (_ : (xs1 @ (_ : xs2)))) =
> zipWith3 (\x y z -> (x+y+z)/3) xs0 xs1 xs2
*Main> moving_average3 [1..10]
The result is two elements shorter than the original, but that
_is_ the definition of moving average after all.
More information about the Haskell-Cafe