I work through F # Wikibook, and I ended up in the section on the reference cells, in which the following code fragment appears:
let incr =
let counter = ref 0
fun () ->
counter := !counter + 1
Then this function is called three times, setting the values 1, 2 and 3, respectively. Can someone explain why this function does not return 1 each time it is called? The way to interpret this code (which is obviously not the correct interpretation, therefore, the question) is that, firstly, a reference to the cell "counter" is declared, with the content equal 0, the contents of the "counter" is increased by 1, and then dereferenced using an anonymous function. Since each call incr();;declares a “counter” with the contents 0, I do not understand why the call incr();;does not just return 1 each time.
Can anyone correct my understanding?
Thanks in advance.
source
share