3 ms·
> The counterpoint there is, tail calls are just rewrites for looping constructs, as Guido admits in point 3 Except they're not. Here are some functions using
by chriswarbo 4y ago
> The counterpoint there is, tail calls are just rewrites for looping constructs, as Guido admits in point 3
Except they're not. Here are some functions using tail-calls:
checks = {
'odd' : lambda n: False if n <= 0 else checks['even'](n-1),
'even': lambda n: True if n <= 0 else checks['odd' ](n-1)
}
These aren't a direct translation of loops, for a few reasons:
- Loops are statements, which can't be used in lambda expressions
- To use statements, we would need to define named functions (using `def`), but that too is a statement
- The names introduced by `def` would need to be unique, to avoid clobbering any existing names. This may require a fresh scope.
- We would need to inline each function's logic into the other
- We would need some intermediate state (separate from the argument 'n') to keep track of whether we're up to even or odd