Bohtvaroh

Blogger - HTIF - join

Oct 4th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- |Joins two sorted lists into list of pairs similarly to SQL full join. O(n)
  2. join :: Ord a => [a] -> [a] -> [(Maybe a, Maybe a)]
  3. join xs ys = join' xs ys []
  4.  where join' [] [] acc = acc
  5.         join' (x:xs) [] acc = join' xs [] ((Just x, Nothing):acc)
  6.         join' [] (y:ys) acc = join' [] ys ((Nothing, Just y):acc)
  7.         join' allXs@(x:xs) allYs@(y:ys) acc =
  8.          let (x', y', xs', ys') = case x `compare` y of
  9.                EQ -> (Just x, Just y, xs, ys)
  10.                GT -> (Nothing, Just y, allXs, ys)
  11.                LT -> (Just x, Nothing, xs, allYs)
  12.          in join' xs' ys' $ (x', y'):acc
Advertisement
Add Comment
Please, Sign In to add comment