In Ocaml, how do I avoid an unused variable warning if I don't want to use the variable?

Viewed 186

Say I'm defining a recursive function on lists that looks something like:

let rec listFunc xs =
    match xs with
    | [] -> aThing
    | h :: t -> listFunc (tailFunc t)
;;

where tailFunc is some other function from lists to lists. The compiler will give me an unused variable warning since I didn't use h, but I can't just use the wildcard since I need to be able to access the tail of the list. How do I prevent the compiler from giving me a warning?

1 Answers

You can prefix the h with an underscore.

let rec listFunc xs =
    match xs with
    | [] -> aThing
    | _h :: t -> listFunc (tailFunc t)
;;

or simply:

let rec listFunc xs =
    match xs with
    | [] -> aThing
    | _ :: t -> listFunc (tailFunc t)
;;

Any binding that starts with _ won't be in scope and won't give you unused variable warnings.

Related