Skip to content

Instantly share code, notes, and snippets.

@Heimdell
Created July 10, 2015 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Heimdell/255ea0515b09cecb0b99 to your computer and use it in GitHub Desktop.
Save Heimdell/255ea0515b09cecb0b99 to your computer and use it in GitHub Desktop.
import Data.List (sortBy)
import Data.Function (on)
merge :: [(Int, a)] -> [(Int, b)] -> [(Maybe a, Maybe b)]
merge az bz = case (az, bz) of
((i, a) : az', (j, b) : bz') ->
case i `compare` j of
LT -> (Just a, Nothing) : merge az' bz
EQ -> (Just a, Just b) : merge az' bz
GT -> (Nothing, Just b) : merge az bz'
(az, []) -> [(Just a, Nothing) | (_, a) <- az]
([], bz) -> [(Nothing, Just b) | (_, b) <- bz]
outerJoin :: [(Int, a)] -> [(Int, b)] -> [(Maybe a, Maybe b)]
outerJoin az bz = sort az `merge` sort bz
where
sort = sortBy (compare `on` fst)
test = outerJoin
[(i, i) | i <- [0,2..100]]
[(i, i) | i <- [0,3..100]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment