title: Returning a function with a closure
body: <p>The following code:</p>
  
  <pre><code>let CreateFunc=
      let counter = ref 0
      fun () -&gt; counter := !counter + 1; !counter
  
  let f1 = CreateFunc
  let f2 = CreateFunc
  
  printfn "%d" (f1())
  printfn "%d" (f1())
  printfn "%d" (f2())
  printfn "%d" (f2())
  </code></pre>
  
  <p>Outputs:</p>
  
  <pre><code>1
  2
  3
  4
  </code></pre>
  
  <p>So, basically, what we see here is f1 and f2 being the same function - as they're obviously sharing the same instance of 'counter'.</p>
  
  <p>The expected output is:</p>
  
  <pre><code>1
  2
  1
  2
  </code></pre>
  
  <p>QUESTION: Shouldn't f1 and f2 be two separate instances? After all they are created by the two different calls to 'CreateFunc'???</p>
  
  <p>Thanks</p>