Caterpillar

Daily Thought - 2024-08-08

< back to list

A few weeks ago, I implemented if, to replace the previous conditional primitives. In the time since, I've discovered shortcomings of if, and have reconsidered my approach to conditionals. I'd like to explain what those shortcomings are, and what other solution I've come up with. But before I can get to that, I need to start with some basics.

So, purely functional languages. If you can't mutate local variables, then there's really no sensible way to have the typical looping constructs from imperative languages. You need recursion. But if you implement recursion naively, then an endless loop will endlessly grow your stack memory use. That won't do.

The solution to that is tail call elimination, and it's quite straight-forward: If a call is the last expression in its scope, then you don't need to create a new stack frame. You don't need to return to the current scope anyway, so you can reuse the already existing stack frame instead. Tomorrow, I want to expand on that, and explain what that has to do with conditionals.

<< previous thoughtnext thought >>