I am very new to Ocaml, although I suspect that you will be able to work this out from the question that I am about to ask.
I am trying to write a recurring function to check if an item is present in a list. I have seen a way to do this that works, but I am confused as to why my solution gives the result of ‘true’, even when the item is not present in the list.
let rec belongs lst x =
match lst with
 -> false
| x::t -> true
| h::tail -> belongs tail x;;
This function correctly returns false if I send it an empty list, but it also always returns true if the list is not empty (along with any random ‘x’) regardless of whether or not x is present in the list.
My hunch is that it is not identifying the ‘x’ in ‘x::t’ with the ‘x’ that is one of the two inputs to the function, but I am puzzled as to why that is the case. I am still new enough to Ocaml to realise that I could be completely wrong in this guess as to why the code is not working properly …
Anyway, any corrections or help much appreciated.