
27 Aug
2009
27 Aug
'09
5:50 p.m.
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
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