"from python import lecture" aka "python_intro_2.pdf"
is the collection of slides for Muli Ben-Yehuda (IBM Haifa Research Labs)
lecture(s?) on basic Python.
Ben-Yehuda has
>>> fac = lambda n:[1,0][n>0] or fac(n-1)*n
Which defines the factorial function in terms of what it does to
a dummy variable n. If n=0 then 0! = 1 by definition. Otherwise
n! = (n-1)! n, a recursive definition.
Now [1,0] is the list with entries 1 and 0. [n>0] is a list of
just one item, ether FALSE or TRUE. But in python that's 0 or 1.
The catenation [1,0][1] is 0 and [1,0][0] is 1. The second case
is just fine, being non-zero, the "or" is 1 (TRUE) regardless of
what nonsense comes afterwards. But if n>0, then the first half
of the "or" is 0, and it evaluates the second.
In C one would write this as n>0 $ fac(n-1)*n : 1; reading
"if n>0 then return fac(n-1)*n, else return 1.
The initialization using the list-catenation feature is nerdy,
especially since fac(-1) returns 1, but (-1)! is undefined.
Nevertheless, the lambda syntax is useful. For example, the
squaring function could be defined as
>>> squ = lambda x: x*x
Exercise. Define the absolute value function.
Appendix: The triple-function in python:
This is too good to be true
[e0,e1,e2,e3,e4][f0,f1,f3][g0]
provided f0,f1,f2 are expressions whose values are integers
in the range of 0..4, the first juxtaposition picks out the
[f0th,f1st,f2nd] and then picks out the g0th, as long as g0
returns a integer in f0..f3. What a language!
This makes bool?e1,e0 = [e0,e1][bool] a quintessentially postfix thingy.