Skip to content

Instantly share code, notes, and snippets.

@rsslldnphy
Created June 24, 2014 16:20
Show Gist options
  • Save rsslldnphy/2e847ca2184c62f0a0e3 to your computer and use it in GitHub Desktop.
Save rsslldnphy/2e847ca2184c62f0a0e3 to your computer and use it in GitHub Desktop.
{-| 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]
-}
import Data.Maybe(catMaybes)
keepEqual :: Eq a => [a] -> [a] -> [a]
keepEqual xs ys = catMaybes $ zipWith match xs ys
where
match x y = if x == y then Just x else Nothing
main :: IO ()
main = do
print $ keepEqual "hello" "world"
print $ keepEqual (repeat 1) ([0..10] :: [Int])
print $ keepEqual [True, False, True] (repeat True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment