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

Type error for a contains element function in OCaml

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?

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

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.

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