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?
>Solution :
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.