I wrote a simple function to return whether an element is in a list.
let rec contains elem l =
match l with
| a::c -> if (a = elem) then true else (contains elem c)
| [] -> false
let t = contains "a" ["a"; "b"];;
Upon executing this code I get the error:
This expression has type string but an expression was expected of type int.
Not really sure what to make of it, why does the interpreter expect the first argument of contains to be of type int?
Strangely when I run the same snippet in an online compiler it works fine. My init file has these directives:
#require "package";; to load a package
#list;; to list the available packages
#camlp4o;; to load camlp4 (standard syntax)
#camlp4r;; to load camlp4 (revised syntax)
#predicates "p,q,...";; to set these predicates
Topfind.reset();; to force that packages will be reloaded
#thread;; to enable threads
and my ocaml version is 4.13.1 (using Ubuntu).
>Solution :
Most likely you are using modules from Jane Street. The Jane Street modules redefine polymorphic comparison operators (such as =) to apply only to int.
See this earlier discussion: Question About OCaml Function Signatures in UTop.