Lewis Clark
Lewis Clark

Reputation: 56

Haskell filter using fst and snd in query

I'm very new to haskell and am trying to filter all the tuples where the first element is bigger than the second. I don't understand why this doesn't work, any help?

main = 
 do
  let xs = [2, 3, 2]
  let ys = [1, 2, 3]
  let cs = zip xs ys
  filter ((>snd).fst) cs

Upvotes: 0

Views: 556

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477180

You can filter with:

filter (\x -> fst x > snd x) cs

but you do not need to use fst or snd in the first place, you can work with uncurry :: (a -> b -> c) -> (a, b) -> c:

filter (uncurry (>)) cs

uncurry takes a function a -> b -> c, and rewrites it to a function that takes a 2-tuple (a, b), and thus calls the function with the elements wrapped in the 2-tuple.

Upvotes: 3

Related Questions