开发者

This expression has type int -> int but is here used with type int

开发者 https://www.devze.com 2023-02-20 22:10 出处:网络
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 sh

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消