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

Popular posts from this blog

Tftp secret of TL-WR740N uncovered

Hidden TFTP of TP-Link routers

Haskell for embedded: C output, compilers, monads, Timber