3 ms·
What is inefficient about the for loop? It does the same amount of comparisons, the same amount of list accesses, and the same amount of assignments to temporar
by yuliyp 7y ago
What is inefficient about the for loop? It does the same amount of comparisons, the same amount of list accesses, and the same amount of assignments to temporaries.
- abjKT26nO8 7y agoI think the OP is referring to the imperative version using recursion as inefficient. It isn't tail-recursive, so the stack will grow to infinity, even though we only need a constant amount of memory (plus the memory to store the list) to calculate the result. Most likely, they will cause the program to crash, when invoked on moderately big input. Most implementations of "reduce" are tail-recursive (and if they're not, that's a bug), so they're more efficient.