[Haskell-cafe] Mapping over multiple values of a list at once?
max.rabkin at gmail.com
Thu Aug 27 04:30:12 EDT 2009
My first approach would be to generate the list of sliding windows:
after importing Data.List:
> map (take 3) . tails $ [4,3,2,6,7]
Not quite what we want, but close:
> filter ((== 3) . length) . map (take 3) . tails $ [4,3,2,6,7]
So (filter ((== 3) . length) . map (take 3) . tails) seems to be the
desired function. Now just map average.
However, we don't really need the sliding windows themselves, just the
sliding sum. There might be a slightly more efficient way to do that,
but I'll leave it as an exercise for you or somebody else.
On Thu, Aug 27, 2009 at 10:19 AM, <haskell at kudling.de> wrote:
> Imagine you have a list with n-values. You are asked to iterate over the list and calculate the average value of each 3 neighbouring values.
> 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?
> The naive ansatz to use "(!!") excessively sounds pretty inefficient.
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe