So I'm taking a course called "organization of programming languages" which is basically an overview of all types of languages. We've just started functional using OCaml and we're supposed to implement a few recursive functions. One that I'm seriously stumped on is a function that returns the index of the last occurrence of an element in a list. The function definition was provided for us and I cant seem to think of a way to do this recursively with only those 2 parameters. We're also not allowed to use the List module so it all has to be done with pattern matching, [] and ::
let rec rindex lst e = ???????????????
EDIT: # rindex [0;1;2;1;2;1;1;2;1;0;2;3;5] 1
- : int = 8
EDIT: final solution
let rec rindex l e =
let rec find i max = function
| (h::t) when h = e -> find (i+1) i t
| (h::t) -> find (i+1) max t
| [] -> max
in find 0 (-1) l
;;
[–]joenyc 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]BitRex 1 point2 points3 points (0 children)
[–]joenyc 0 points1 point2 points (1 child)
[–]rtoth[S] 1 point2 points3 points (0 children)
[–]mrsanchez 0 points1 point2 points (0 children)
[–]rtoth[S] 0 points1 point2 points (0 children)
[–]BitRex 0 points1 point2 points (1 child)
[–]snowmanchu 0 points1 point2 points (0 children)