3 ms·
A slightly different viewpoint: a local array of 256 elements is, semantically speaking, no different than 256 separate variables. Assuming that we had 256 regi
by derefr 18y ago
A slightly different viewpoint: a local array of 256 elements is, semantically speaking, no different than 256 separate variables. Assuming that we had 256 registers laying around to keep them in, tail-call optimizing a 256-variable recursion would just leave those values in their registers between loops. And, since the stack is just a place to hold registers we aren't paying direct attention to, we shouldn't treat the 256 variables any differently because they're on the stack instead. Thus, mutability is actually the correct tail-call optimization in this case.
- silentbicycle 18y agoWhile it's just an implementation detail, some languages (off the top of my head: Chicken Scheme and Lua) can't use that many variables in their tail-call implementations.
- mononcqc 18y agoWith Erlang, a similar effect could be achieved with the process dictionary: > erlang:put(1,1). undefined > erlang:get(1). 1 > erlang:put(1,erlang:get(1)+1). 1 > erlang:get(1). 2 > erlang:put(2,3). undefined > erlang:get(). [{1,2},{2,3}] It's limited to a single process and thus doesn't break the 'no-shared-memory' model. It also lets you access and modify data without copying it, so the closest implementation of such a function would need to be defined that way and would only return the final list. Of course, this breaks the idea of 'purely functional' which is what the author found problematic.