[Haskell-cafe] Mapping over multiple values of a list at once?
ekirpichov at gmail.com
Thu Aug 27 05:19:32 EDT 2009
How about this one? Should be pretty efficient.
let mavg n xs = let (sum -> seed,rest) = splitAt n xs in map (%n) .
scanl (\a (p,n) -> a+n-p) seed $ xs `zip` rest
2009/8/27 Patai Gergely <patai_gergely at fastmail.fm>:
>> For example, starting from
>> you need to find the averages of
>> 4,3,2 and 3,2,6 and 2,6,7
>> resulting in
>> What is the most elegant way to do that?
> It's probably less elegant than tails, but very likely more efficient to
> keep track of running sums instead of summing the sublists over and over
> import Data.Ratio
> nsums n xs = map (% n) $ scanl (+) (sum (take n xs)) $ zipWith (-) (drop
> n xs) xs
> http://www.fastmail.fm - The professional email service
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
Web IR developer, market.yandex.ru
More information about the Haskell-Cafe