Elementwise processing 2.1 for common keys in Haskell
Here's a neat little utility function which I commonly find useful.
ef2_1 :: (a -> a -> a) -> (a -> a -> Bool) -> [a] -> [a] -> [a]
ef2_1 = ef0 [] where
ef0 zs f lt (x:xs) (y:ys) | lt x y = ef0 (x:zs) f lt xs (y:ys)
ef0 zs f lt (x:xs) (y:ys) | lt y x = ef0 (y:zs) f lt (x:xs) ys
ef0 zs f lt (x:xs) (y:ys) = ef0 (f x y:zs) f lt xs ys
ef0 zs _ _ xr yr = reverse $ xr ++ yr ++ zs
main = print $ ef2_1 (\x _->x) (\(x,_) (y,_)->x<y) d1 d2 where
d1 = [(1,9), (3,8), (4,7), (5,6), (7,5)]
d2 = [(2,5), (4,4), (6,3)]
Comments
Post a Comment