edit: this is called a continuation style . Each recursive call builds its continuation function and passes it to the next recursive call, which will be used as this call is selected (depending on whether it is basic or not).
Just write down the recovery steps:
repeat_cont 4 "xo" id repeat_cont 3 "xo" k1 where k1 x = id ("xo" + x) repeat_cont 2 "xo" k2 where k2 x = k1 ("xo" + x) repeat_cont 1 "xo" k3 where k3 x = k2 ("xo" + x) repeat_cont 0 "xo" k4 where k4 x = k3 ("xo" + x) k4 "" k3 ("xo" + "") k2 ("xo" + ("xo" + "")) k1 ("xo" + ("xo" + ("xo" + ""))) id ("xo" + ("xo" + ("xo" + ("xo" + "")))) "xoxoxoxo"
Each ki continuation function is “what to do with the result that will be obtained from the recursive call”.
Recursive cases, ki , say "any recursive result x that I give, add s to it and pass the increased chain up the chain as a new, changed result."
The most external, id , simply says: "Returns the (final) result as".
When the base case 0 reached, the continuation function k4 been built and is ready to accept its argument in order to do its job. He will add the string "xo" to his argument and pass the result along the chain of continuation functions to k3 . The argument will be used in "xo" + x , so it must be a string.
Adding "" to a string is an identification operation, so in the basic case it says: "Just let the chain of continuation functions do their work without any additional interference from me."
NB: I was careful to always say “continuation function” to avoid confusion with first-class continuations , which are generally different and much more powerful beasts (not sure if F # has them, though).
source share