I am new to OCaml and am writing a recursive program in OCaml which returns the n-th element of a list. However I need to display an informative error message, displaying a list, when the list is too short, like "( a b c ) does not have 5 elements". Here's my code
let rec nth_element n list =
match list with
| [] -> raise(Failure "")
| a :: l -> match n with
0 -> a
n -> nth_element (n-1) l
I want to replace the 'raise(Failure "")' part with the required error message. Writing a function for the same didn't help as it returned the unit type whereas the int type is required.