title: Implementation of Array.length in OCaml
body: <p>I want to understand how <a href="https://v2.ocaml.org/api/Array.html" rel="nofollow noreferrer">Array.length</a> is implemented. I managed to write it with <code>Array.fold_left</code>:</p>
  <pre><code>let length a = Array.fold_left (fun x _ -&gt; x + 1) 0 a
  </code></pre>
  <p>However in the <a href="https://github.com/ocaml/ocaml/blob/trunk/stdlib/array.ml" rel="nofollow noreferrer">standard library</a>, <code>fold_left</code> uses <code>length</code> so that can't be it. For <code>length</code> there is just this line in the stdlib which I don't understand:</p>
  <pre><code>external length : 'a array -&gt; int = &quot;%array_length&quot;
  </code></pre>
  <p>How can I write <code>length</code> without using<code>fold_left</code>?</p>
  <p><strong>EDIT:</strong></p>
  <p>I tried to do it with pattern matching, however it is not exhaustive, how can I make the matching more precise? (The aim is to remove the last element and return i+1 when only one element is left)</p>
  <pre><code>let length a = 
           let rec aux arr i = 
           match arr with
             | [|h|] -&gt; i+1
             | [|h;t|] -&gt; aux [|h|] (i+1)
          in aux a 0;;
  </code></pre>