3 ms·
Huh, I already thought this was suspicious reading the title, as Python does not have in-built TCO and indeed, https://composingprograms.com/pages/17-recursive-
by zelphirkalt 8y ago
Huh, I already thought this was suspicious reading the title, as Python does not have in-built TCO and indeed, https://composingprograms.com/pages/17-recursive-functions.html https://composingprograms.com/pages/17-recursive-functions.h... does not even mention Stackoverflow errors and neither does it introduce a decorator to avoid them.
Under these conditions however, it is impossible to compose programs "SICP Style".
- lmz 8y agoNone of the examples there in that section are tail recursive anyway. It has one example of a while loop for iteration.
- argv_empty 8y agoMakes "SICP Style" seem like overselling it a bit.
- abecedarius 8y agoWhen I worked through SICP in the 80s it was with XLisp on MS-DOS. XLisp didn't optimize tail recursion, and I had well under a megabyte, but this turned out to be a problem only once or twice, and ad-hoc hacks were enough then. (OTOH the system didn't have an only 1000-deep call-stack limit by default, either.) It's true that this'd be more of a problem for real-world work.
- e12e 8y agoThey introduce both a traditional factorial function and a naive Fibonacci function - it seems likely most enterprising students will overflow the stack. Odd that they don't (from a quick skim) seem to mention it at all? When they do hand wave a bit around stack frames (call trees)?
- abecedarius 8y agoA naive fib(n) only needs stack depth of about n -- it takes a long time but doesn't need to go very deep.
- e12e 8y agoYou're right about Fibonacci - a naive fib(1000) takes a long time to eventually overflow the stack in python. But fact(1000) quickly crashes. Which is a shame as python has a full(?) number tower and has no problem calculating 1000!