ocaml

How to find minimum of a tuples list


I have to find a minimum of a tuples list but I only want to return the minimum of snd element of the tuples, not the entire tuple. Unfortunately i'm having the following error in the following code and I don't know why. The l argument is a list of tuples (float * float)

let rec minRight l = match l with
    | [] -> raise (Arg.Bad "minRight: empty list")
    | [x]-> x
    | (_,y)::xs -> min y (minRight xs)

Error:

| (_,y)::xs -> min y (minRight xs)
Error: This expression has type 'a but an expression was expected of type
         'b * 'a

Thanks in advance.


Solution

  • Here is one problem, in addition to the ones mentioned in previous answers: the line

    | [x]-> x
    

    returns a tuple, while you said you want to "return the minimum of snd element[s] of the tuples".