Skip to content

Instantly share code, notes, and snippets.

@blouerat
Created June 20, 2014 07:43
Show Gist options
  • Save blouerat/3d97e87be4889e19147a to your computer and use it in GitHub Desktop.
Save blouerat/3d97e87be4889e19147a to your computer and use it in GitHub Desktop.
1HaskellADay: keepEqual
{-| Keep equal elements that are at the same position in both lists
Examples:
>>> keepEqual "hello" "world"
"l"
>>> keepEqual (repeat 1) [0..10]
[1]
>>> keepEqual [True, False, True] (repeat True)
[True,True]
-}
keepEqual :: Eq a => [a] -> [a] -> [a]
keepEqual a b = map fst $ (filter $ uncurry (==)) $ zip a b
-- With more curry, just like Freddy.
keepEqual' :: Eq a => [a] -> [a] -> [a]
keepEqual' = curry $ map fst . (filter $ uncurry (==)) . uncurry zip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment