[Haskell-cafe] Mapping over multiple values of a list at once?

Stephen Tetley stephen.tetley at gmail.com
Thu Aug 27 13:50:17 EDT 2009


Hi Max

How about a paramorphism?


slideAvg3 :: [Int] -> [Int]
slideAvg3 = para phi [] where
  phi x ((y:z:_),acc) = average3 x y z : acc
  phi x (_,acc)       = acc

-- helpers

-- paramorphism (generalizes catamorphism (fold))
para :: (a -> ([a], b) -> b) -> b -> [a] -> b
para phi b []     = b
para phi b (x:xs) = phi x (xs, para phi b xs)


average3 :: Int -> Int -> Int -> Int
average3 a b c = round $ (fromIntegral $ a+b+c)/3

I haven't tested for efficiency though.

Best wishes

Stephen


2009/8/27 Max Rabkin <max.rabkin at gmail.com>:

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


More information about the Haskell-Cafe mailing list