tricks
DESCRIPTION
Stupid Programming Tricks, a talk I gave at the Portland Python Meetup 4/10/2012TRANSCRIPT
Stupid Programming Tricks (Part 1)
Stupid Programming Tricks
Some programming tricks are so cool yet utterly inane. Let's talk about some stupid things....
The Y-Combinator
You may be asking yourself: What the hell is Y-Combinator?
First some definitions...
● Functions that can be passed as values● The calculus of function composition● Most importantly, functions can take other
functions
Some terminology
λx.x == lambda x: xfunctional - a function that takes other functions, and returns a functionCombinator - a function that takes functionals (functions that take other functions) and returns a function
Fixed points
Fixed points of a function are any values for x such that f(x) = x. Consider f(x) = x^2 - it's fixed points are 0, 1. If we can compute the fixed points of a function, why not a functional?
Fixed points
What would the fixed point of a functional look like? f(g) = g where g is a function. The result would be a function not a value.
Y-Combinator
The Y combinator simply computes the fixed point of a function such that:Y(F) = F(Y(F)) But what does this mean?
Magic.
The Y-Combinator can compute the fixed point of a recursive function without using recursion. Y(F) = F(Y(F)) is pretty useless in most languages. Expanding we get:Y = λf.(λx.f (x x)) (λx.f (x x)) Note there's no longer a reference to Y on the RHS.
Show me the code.
Let's look at a python example....
Cool.
The Y-Combinator is pretty cool but ultimately pretty impractical.Questions?
[email protected]://gist.github.com/2348175
@zbskii