Hacker News new | ask | show | jobs
by Smaug123 2184 days ago
I've got a blog post in the oven about tail-call recursion and how to use it in F# to write your loops more safely. By using recursive (possibly side-effectful) functions instead of iteration, you make the decision about "whether or not to loop" explicit in code, making it much harder to make e.g. certain classes of off-by-one errors. When it's an explicit decision to loop, by calling yourself with an amended immutable state parameter, it often sticks out like a sore thumb when you loop wrongly (e.g. by forgetting to update your state, or by looping one too many times).