Re:Don’t Think, Just Defunctionalize
by recursivecaveat on Wednesday December 23, 2020 @09:31PM
(#25524543)
I tend to think of it as a handy feature of recursive algorithms that encourages me to use them. Just twice in my life have I wanted to exceed the default stack size in Python, but many times have my programs crashed with a helpful stack trace when they would otherwise have locked up in an infinite loop. I do however work in a subfield where it's pretty acceptable to just say "yaaa, don't do that" to unbounded input sizes, so ymmv.
[ Parent
| on HN ]
Re:Don’t Think, Just Defunctionalize
by chriswarbo on Tuesday January 05, 2021 @06:39AM
(#25644484)
There are many situations where we want infinite recursion. A simple example is a server:
def serve(request):
response = process(request)
socket.send(response)
serve(socket.receive())
The same thing applies to anything which 'handles events', including servers, games, GUIs, CLIs, etc. In general, any time we might write a "main loop" like: foo = lambda z: ....
bar = lambda x: ....
baz = lambda y: ....
def main():
z = init
while true:
x = foo(z)
y = bar(x)
z = baz(y)
This is equivalent to having each step perform a tail-call to the next one: foo = lambda z: bar(....)
bar = lambda x: baz(....)
baz = lambda y: foo(....)
main = lambda : foo(init)
The latter form has better encapsulation, requires no mutable state, doesn't have stale/out-of-date variables, etc.The fact that infinite recursion is seen as a bug, yet "while true:" loops are common, is a symptom of this negative feedback loop which keeps people from learning and using recursive solutions.