OCaml - Accumulator Using Fold Left
<p>Learning OCaml from <a href="https://try.ocamlpro.com/" rel="nofollow">here</a>.</p>

<p>I want to verify if I have understood how this snippet of OCaml code works</p>

<pre><code>List.fold_left (fun acc x -&gt; acc + x) 0 [ 1; 2; 3; 4 ]
</code></pre>

<p>I have an intuition that this is an equivalent to the <code>reduce</code> function in Python. Specifically, I think it is equivalent to </p>

<pre><code>reduce(lambda x, y: x + y, [1, 2, 3])
</code></pre>

<p>The anonymous function is taking two parameters - <code>acc</code> and <code>x</code> and returns a single value <code>acc + x</code>. I understand that initially, the first argument <code>acc</code> will be 0 but how does it know that the second argument has to be the first element of the list? </p>

<p>What I think is happening is that <code>fold_left</code> provides the two arguments to the anonymous function and then recursively calls itself with new arguments until the list becomes empty.</p>

<p>To confirm this I saw <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html" rel="nofollow">this</a>.</p>

<p>When I define a function like <code>let inc x = x + 1</code> I get something like <code>val inc : int -&gt; int = &lt;fun&gt;</code> but in this case the signature is <code>: ('a -&gt; 'b -&gt; 'a) -&gt; 'a -&gt; 'b list -&gt; 'a = &lt;fun&gt;</code></p>

<p>What is <code>'a</code> and how should I interpret this function signature so that <code>List.fold_right f [a1; ...; an] b</code> becomes <code>f a1 (f a2 (... (f an b) ...))</code>?</p>