stupid python tricks

6 thoughts
last posted April 25, 2016, 8:24 a.m.
1

Norvig's reboot of Strachey's 1966 checkers program (ab)uses default arguments to map CPL's 1960's era use of let-binding. Now, there are other stupid tricks one can play with binding in python, but my current favorite involves misuse[0] of comprehensions.

With a bit of effort, we even have letrec:

print(min( f(6) 
    for Y0  in [lambda y,f:
                lambda *x: f(y(y,f))(*x)]
    for f   in [Y0(Y0,lambda fac:
                lambda n: 1 if n<2 else
                          n*fac(n-1))]))

Exercise 1: add one line to produce the classic Y

Exercise 2*: modify Y0 to support the classic mutual recursion between even and odd

[0] or not exactly misuse? cf Wadler's Comprehending Monads (p19,infra):

[the existence of the map from the identity to the list monad] explains a trick occasionally used...

5 later thoughts