Created
June 24, 2014 16:20
-
-
Save rsslldnphy/2e847ca2184c62f0a0e3 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
{-| 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