[Tutor] recursion and trampolines
dyoo at hkn.eecs.berkeley.edu
Fri Aug 27 21:08:22 CEST 2004
[Note: my post below is slightly advanced, and highly impractical.
On Fri, 27 Aug 2004, Guillermo Fernandez Castellanos wrote:
> I readed the recursion and trampolines, and I had a (very quick...) look
> at the article. Though, there is a few points I don't really get...
> What does this line exactly do?
> -> bouncer = bouncer(*bouncer)
It's an ugly hack. *grin*
I used a list to simulate a "union" data structure with either two or
three fields. This "bouncing" data structure is either:
['bounce', someFunction, argumentsToThatFunction]
So I'm using the first field as a kind of 'type tag', and the remainder of
the list depends on which kind of bouncer the code is generating. Python
doesn't natively have this kind of "discriminated union" type, so the code
here simulates it with lists.
If we write two helper functions to make it more readable:
then the snippet:
bouncer = bouncer(*bouncer)
can be rewritten to:
bouncer = apply(getBounceFunction(bouncer),
[rest of the bounce code cut]
> Well... I have to admit that this, to me, is not much different from a:
> while i>0:
> specially when we see the arguments passed to the function 'bounce' (see
> my execution before):
> (4, 5)
> (3, 20)
> (2, 60)
> (1, 120)
> (0, 120)
Yes, exactly right. That 'while' loop ends up being equivalent to the
bouncing factorial function. A Lisp or Scheme programmer would say that
loops and recursion do the exact same thing. That's why there aren't any
native looping statements in the Scheme language: Scheme programmers do
everything with recursion. It's a weird thought at first, but the code
above proves that it can work.
> Where is the advantage of such trampolin schema that you described? I am
> sure there is one. I simply don't see it :-(
No, your first instincts were right. Part of it is purely academic and
just for my personal amusement: I think it's funny to think about a
bouncing function. *grin*
I have to clarify: most programmers don't ever have to do anything like
this: I'm definitely not saying for people to program this way. This is
certainly not practical! But as an academic exercise, I thought it was
fun to write.
There is a neat thing about trampolines: it's possible to simulate threads
with them. As a concrete example:
"""Bouncers can be of two types: a 'bounce' or a 'land'."""
def bounce(function, *args):
return ["bounce", function, args]
return ["land", value]
"""A big trampoline keeps its bouncers up in the air until one of
bouncer_queue = bouncers[:]
bouncer = bouncer_queue.pop()
bouncer = bouncer(*bouncer)
if bouncer == 'land':
print "bouncer finished:", bouncer
print "exiting the trampoline!"
print "hello world"
print "hola world"
if n == 5:
return bounce(tramped_countUpToFive, n+1)
if __name__ == '__main__':
baby_bouncers = [bounce(tramped_sayHello),
When we run this program, we can see a surprising result:
bouncer finished: ['land', None]
exiting the trampoline!
In effect, we're threading between the three functions! Trampolines show
that, in theory, threaded programming can be done without OS support.
Again, no one in their right mind would ever write threads this way, by
Hope this was interesting!
More information about the Tutor