Created
August 13, 2017 02:09
-
-
Save dhanji/088fb1d49ca3f891a326d81d60e2a73c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is this gobbledygook?!