Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ozgurakgun
Created February 17, 2010 16:35
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 ozgurakgun/306782 to your computer and use it in GitHub Desktop.
Save ozgurakgun/306782 to your computer and use it in GitHub Desktop.
unionAll :: (Ord a) => [[a]] -> [a]
unionAll xs =
let
xs' = filter (not.null) xs -- | filter empty lists, if any
in
if null xs' -- | if nothing is left after filtering
then [] -- | return the empty list
else -- | otherwise
let
next = minimum (map head xs') -- | find the minimum of head elements
-- | they are minimum values in their respective lists
-- | so this is the minimum of every list
xs'' = map (dropWhile (next==)) xs' -- | remove that value from every list
-- | (no need to filter this time, since lists are ordered)
in next : unionAll xs'' -- | recursively call unionAll on xs''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment