Open anfelor opened 12 months ago
It certainly does depend on your definition of "straightforwardly"! I think that CPS and defunctionalization are good topics for a second book on functional programming, but not a first one. Similarly, zippers as a first-order reification of a delimited continuation is a wonderful way to see the world, but I think that it's too much for the audience here.
I'll leave the issue open, because if you tripped over "straightforwardly" here, then someone else probably will too, so I should consider a more precise formulation like "using the tools and ideas presented in this book".
Thanks for the links to the papers - they look very interesting.
Please quote the text that is incorrect:
In what way is this incorrect?
I guess it depends on your definition of straightforwardly, but there is certainly a version using accumulators (pseudo-code, sorry):
(you can avoid mutual recursion by using a boolean to decide which function you are in).
How do you get there? I agree that it might out of the scope of the book, but it turns out to be quite simple: You just perform continuation-passing-style and then defunctionalize the closures 1. This technique always works (since CPS always works) and also often yields fast code in languages like Lean as it nicely makes use of reuse analysis to be fully in-place 2.