Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading