I implemented a simple state machine in Python:
import time
def a():
print \"a()\"
return b
def b():
print \"b()\"
return c
def c():
print
The Python code you posted will be transformed into a recursive function, but it will not be tail call optimized because Python has no tail call optimization, so it will stack overflow at some point. So the Python code is actually broken, and would take more work to get it as good as the Haskell or C versions.
Here is an example of what I mean:
so.py:
import threading
stack_size_bytes = 10**5
threading.stack_size(10**5)
machine_word_size = 4
def t1():
print "start t1"
n = stack_size_bytes/machine_word_size
while n:
n -= 1
print "done t1"
def t2():
print "start t2"
n = stack_size_bytes/machine_word_size+1
while n:
n -= 1
print "done t2"
if __name__ == "__main__":
t = threading.Thread(target=t1)
t.start()
t.join()
t = threading.Thread(target=t2)
t.start()
t.join()
shell:
$ python so.py
start t1
done t1
start t2
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 530, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 483, in run
self.__target(*self.__args, **self.__kwargs)
File "so.py", line 18, in t2
print "done t2"
RuntimeError: maximum recursion depth exceeded