There is a template in Javascript called a sample of a Russian doll (this can also be called "one shot"). Basically, this is a function that at some point replaces itself with another.
A simple example:
var func = function(){
func = function(){ console.log("subsequent calls call this...");};
console.log("first call");
}
So, the first time you call func, it issues the "first call", and the next (and the next time) prints "subsequent calls that cause this ...". (this would be easy to do in the Scheme, for example)
I was puzzled by how to do this in Ocaml?
Edit: one of the solutions I came up with:
let rec func = ref( fun () -> func := ( fun () -> Printf.printf("subsequent..\n"));Printf.printf("First..\n"));;
Called as:! func () ;;
Interestingly, if I do not include "rec" in the definition, it never calls the subsequent function ... It always prints "First ...".