This expression has type int -> int but is here used with type int
Write a function
rindex l e : 'a list -> 'a -> int
that takes a list and a single element and returns the position of the last occurrence of that element in the list (indexed by zero). You should return -1 if the element is not in the list.
This is my attempt, but I get the error “This expression has type int -> int but is here used with type int
”. What's worong?
let rec finderd l e n r=
match l with [] -> r
|(h::t) -> if h=e then (finderd t e (n+1) n) else (finderd t e (n+1) r);;
let r开发者_StackOverflowindex l e =(finderd l e 0 -1);;
This is a very common downside of the syntax. Should be:
let rindex l e =(finderd l e 0 (-1));;
Otherwise it recognizes 0 -1
as an expression.
精彩评论