Domanda

I am trying to implement the following:

let list = [1;2;3;4];;

if ((List.exists 3 list) = true)
 print_string "element exists in list\n"

But it is giving me the error: This expression has type int list but an expression was expected of type 'a -> bool

I am not sure what this means.

È stato utile?

Soluzione

List.exists takes a function and a list, not a value and a list. For testing whether a value is in a list, use List.mem.

Your if looks like C syntax. In OCaml you need to use then (but you don't need the parentheses).

As a side comment, if e = true then ... is the same as if e then .... If you use good names for things, the latter is usually clearer.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top