-
Notifications
You must be signed in to change notification settings - Fork 1
/
10_uncurry.hs
31 lines (25 loc) · 1.02 KB
/
10_uncurry.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-- Here's where Haskell starts to get awesome. Let's imagine you have a list of
-- integer tuples, like [(1, 2), (3, 3), (4, 5), (6, 6)] and you want to get rid
-- of the ones that are duplicated (ie (3, 3) and (6, 6)). You might start with
-- 'fst' and 'snd' functions like so:
--
-- nonMatching :: [(Int, Int)] -> [(Int, Int)]
-- nonMatching = filter (\x -> (fst x) /= (snd x))
--
-- But there's a slick way to do that. Note that '/=' is a binary function that
-- returns a boolean:
-- (/=) :: a -> a -> Bool
--
-- You can use 'uncurry' to turn that a -> b -> c function into something that
-- works on pairs! Check out its type signature:
-- uncurry :: (a -> b -> c) -> (a, b) -> c
noMatches :: [(Int, Int)] -> [(Int, Int)]
noMatches = filter $ uncurry (/=)
-- => [(1,2),(4,5)]
-- If you want to keep the matches:
onlyMatches :: [(Int, Int)] -> [(Int, Int)]
onlyMatches = filter $ uncurry (==)
-- => [(3,3),(6,6)]
-- From there, you can see how it would be easy to join a touple of lists:
merge :: ([a], [a]) -> [a]
merge = uncurry (++)