title: OCaml recursive function to apply a function n times
body: <p>I want to create a function of type int -> ('a -> 'a) -> 'a -> 'a in OCaml that takes an int n (non-neg) and a function f 'a -> 'a and an argument a of type 'a. f should be called on a n times.</p>
  
  <p>I've tried 3 different things but can only get int -> ('a -> 'b) -> 'a -> 'b, here are a few things I've tried.</p>
  
  <pre><code>let rec f n g a = 
  g a;
  f (n-1) g a;;
  </code></pre>
  
  <p>which gives</p>
  
  <pre><code>val f : int -&gt; ('a -&gt; 'b) -&gt; 'a -&gt; 'c = &lt;fun&gt;
  </code></pre>
  
  <p>and I've tried </p>
  
  <pre><code>    let rec f n g a =
    if n &gt; 0 then f (n-1) g a
    else g a
    ;;
  </code></pre>
  
  <p>which gave me</p>
  
  <pre><code>val f : int -&gt; ('a -&gt; 'b) -&gt; 'a -&gt; 'b = &lt;fun&gt;
  </code></pre>
  
  <p>The second is closer but I'm at a loss for how to get int -> ('a -> 'a) -> 'a -> 'a</p>