Reputation: 71
Why is my code wrong?
# let ls = [1;2];;
val ls : int list = [1; 2]
# let inList a l = List.exists a l;;
val inList : ('a -> bool) -> 'a list -> bool =
# inList 1 ls;;
Error: This expression has type int but an expression was expected of type 'a -> bool
Upvotes: 1
Views: 12676
Reputation: 4441
Well, as you can see :
# let inList a l = List.exists a l;;
val inList : ('a -> bool) -> 'a list -> bool
So a
is of type 'a -> bool
which means that a
is a predicate on each element of the list.
What you wanted to write was
let inList a l = List.mem a l
val inList : 'a -> 'a list -> bool
TL;DR RTFM ;-) http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html
Upvotes: 1
Reputation: 66818
The first parameter of List.exists
is a function that returns true if the element is one you're looking for and false if not. You're supplying the int 1
, which isn't a function.
You need a function looking_for
like this:
let inList a l =
let looking_for x = ... in
List.exists looking_for l
The function looking_for
should return true if x
is what you're looking for (i.e., if it's equal to a
) and false otherwise.
Upvotes: 2