Karavana
Karavana

Reputation: 487

how to filter the minimums haskell

now I already have a function that takes the minimum of the list of tuples' first element, for example;

mymin [(3,4),(3,2),(4,3)] = 3

By using this function, I'd like to take all the tuples which has 3 as its first element. I tried to filter the ones that has 3 on its first element but;

filter (\a -> mymin (x:xs) == fst x) (x:xs)

which gives

[(3,4),(3,2),(4,3)]

again because everytime it cuts the list, it finds mymin again, but I just want to take the

[(3,4),(3,2)]

part, what track should I follow, I stuck. Thanks for any help.

Upvotes: 0

Views: 166

Answers (2)

kaan
kaan

Reputation: 796

You only have to replace x with a in

filter (\a -> mymin (x:xs) == fst x) (x:xs)

(fst a instead of fst x)

Upvotes: 1

Nikita Volkov
Nikita Volkov

Reputation: 43310

Why not use let or where to precompute the minimum value prior to filtering based on it?

yourFilter list = 
  let m = yourMin list
  in filter (\(a, _) -> a == m) list

Alternatively, with a point-free style lambda:

yourFilter list = 
  let m = yourMin list
  in filter ((== m) . fst) list

Upvotes: 8

Related Questions