title: Keeping a counter at each recursive call in OCaml
body: <p>I am trying to write a function that returns the index of the passed value <code>v</code> in a given list <code>x</code>; -1 if not found. My attempt at the solution:</p>
  
  <pre><code>let rec index (x, v) =
      let i = 0 in
          match x with
          [] -&gt; -1
          | (curr::rest) -&gt; if(curr == v) then
                              i
                            else
                              succ i; (* i++ *)
                              index(rest, v)
  ;;
  </code></pre>
  
  <p>This is obviously wrong to me (it will return -1 every time) because it redefines <code>i</code> at each pass. I have some obscure ways of doing it with separate functions in my head, none which I can write down at the moment. I know this is a common pattern in all programming, so my question is, what's the best way to do this in OCaml?</p>