Skip to content

Instantly share code, notes, and snippets.

@dhanji
Created August 13, 2017 02:09
Show Gist options
  • Save dhanji/088fb1d49ca3f891a326d81d60e2a73c to your computer and use it in GitHub Desktop.
Save dhanji/088fb1d49ca3f891a326d81d60e2a73c to your computer and use it in GitHub Desktop.
-- Intersect an arbitrary number of ordered lists
-- based on a problem by Alec Thomas
intersectTwo [] [] = []
intersectTwo ls [] = []
intersectTwo [] ls = []
intersectTwo (x:xs) (y:ys)
| x == y = [x] ++ (intersectTwo xs ys)
| x > y = intersectTwo (x:xs) ys
| x < y = intersectTwo xs (y:ys)
intersect [] = []
intersect ls = foldr intersectTwo (ls !! 0) ls
-- Intersect as many lists as you like here!
main = putStrLn $ show $ intersect [[1, 2, 3, 4], [2, 3], [2, 3, 4], [2, 3, 4, 6]]
@alecthomas
Copy link

What is this gobbledygook?!

@dhanji
Copy link
Author

dhanji commented Aug 13, 2017

@alecthomas civilized coding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment