0

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.

4

2 回答 2

8

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
于 2013-03-30T13:44:35.510 回答
1

You only have to replace x with a in

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

(fst a instead of fst x)

于 2013-03-30T13:58:15.217 回答