Re:Tree Calculus
by calf on Tuesday December 10, 2024 @06:30PM
(#42382894)
I'm rusty but doesn't the increase depend differently, the second adjust lets you decide to assign a value for the variable increase much later, whereas the first adjust fixes the value of increase at the point of evaluating that very assignment statement. There's a name for this, dynamic scoping or something.
[ Parent
| on HN ]
Yes, the two are not identical - in the former, increase is not the part of the closure, in the latter it is.
Test:
increase = True
adjust1 = (lambda x: x + 1) if increase else (lambda x: x - 1)
adjust2 = lambda x: x + 1 if increase else x - 1
print(adjust1(42))
print(adjust2(42))
increase = False
print(adjust1(42))
print(adjust2(42))
Re:Tree Calculus
by chriswarbo on Wednesday December 11, 2024 @07:41AM
(#42387159)
Yes, my argument relies on immutability of all values (including `increase`). Also, under lazy evaluation we can call `adjust2` immediately, since we know it's a `lambda`; yet attempting to call `adjust1` will force evaluation of `increase`; not good if `increase = provable(collatz_conjecture)`!