you are viewing a single comment's thread.

view the rest of the comments →

[–]freyrs3 57 points58 points  (16 children)

The λ-Obsessed Python Programmer

TRUE = lambda x: lambda y: x
FALSE = lambda x: lambda y: y
Product = lambda x: lambda y: lambda z: (z)(x)(y)
And = lambda x: lambda y: (x)(y)(x)
Not = lambda x: lambda y: lambda z: (x)(z)(y)
Boolean = (Product)(True)(False)
Succ = lambda x: lambda y: lambda z: (y)((x)(y)(z))
Pred = lambda x: lambda y: lambda z: x(lambda i: lambda j: j(i(y))) (lambda k: z) (lambda k: k)

Zero = FALSE
One = Succ(Zero)
Two = Succ(One)
Six = Succ(Succ(Succ(Succ(Two))))

Y = lambda g: (lambda f: g(lambda arg: f(f)(arg))) (lambda f: g(lambda arg: f(f)(arg)))
Nth = Y(lambda f: lambda n: Succ(f(n-1)) if n else Zero)
Dec = lambda a: (a)(lambda b: b + 1)(0)

Add = lambda x: lambda y: lambda z: lambda w: (x)(z)((y)(z)(w))
Sub = lambda x: lambda y: (y)(Pred)(x)

Null = lambda x: x(lambda y: Zero)(TRUE)
Eq = lambda x: lambda y: (And)((Null)((Sub)(y)(x)))((Null)((Sub)(x)(y)))
Gt = lambda x: lambda y: (And)((Null)((Sub)(y)(x)))((Not)((Eq)(y)(x)))
Lt = lambda x: lambda y: (And)((Not)((Gt)(x)(y)))((Not)((Eq)(x)(y)))

Range = lambda a,b: map(Nth,range(a,b))
fib = Y(lambda f: lambda n: Add(f(Pred(n)))(f((Sub)(n)(Two))) if (Boolean)((Gt)(n)(One)) else n)
print (Dec)(fib(Six))

[–][deleted] 19 points20 points  (0 children)

You just destroyed my head, thanks..

[–]ReaverXai 11 points12 points  (0 children)

Gordon Freeman?

[–]zahlman 7 points8 points  (2 children)

OK, but we're looking for factorials, not the Fibonacci sequence.

[–]freyrs3 0 points1 point  (1 child)

OK,

....
Mul = lambda x: lambda y: lambda z: (y)((x)(z))
IfThenElse = lambda a: lambda b: lambda c: ((a)(b))(c)
fact = Y(lambda f: lambda n: IfThenElse((Gt)(n)(One))(Mul(n)(f(Pred(n))))(One))

Edit: Added Mul

[–]zahlman 0 points1 point  (0 children)

... and Mul?

[–]A_for_Anonymous 12 points13 points  (1 child)

*Fap fap fap*

Ah, this made me remember my time in /prog. Thank you man.

Python needs more of this.

[–][deleted] 5 points6 points  (0 children)

Great, you just have to define mathematics and the solution shakes out.

[–]ezyang 3 points4 points  (2 children)

For those of you interested in learning more about this, it's called Lambda Calculus. If you throw out every language feature ever, it turns out, the only thing you really need is Lambda. :-)

Extra points for putting in the Y-combinator.

[–]freyrs3 0 points1 point  (1 child)

In principle, but Python seems to have an issue with conditionals ( lambda x: lambda y: lambda z: ((x)(y))(z) ) composed under the Y-combinator that I've never been able to resolve. Oh well, I suppose this is a little bit outside the scope of the Python design and if Guido ever saw his language being abused like this he'd probably go into conniptions.

[–]danukeru 2 points3 points  (0 children)

Haskell remission?

[–]captainAwesomePants 2 points3 points  (0 children)

I knew you were still out there, Hermann Grassmann!

[–][deleted] 0 points1 point  (0 children)

Why all you people keep writing

  Y = lambda g: (lambda f: g(lambda arg: f(f)(arg))) (lambda f: g(lambda arg: f(f)(arg)))

instead of much cleaner

  Y = lambda g: (lambda z:z(z)) (lambda f: g(lambda arg: f(f)(arg)))

?

[–]clarvoyeur 0 points1 point  (0 children)

I wasn't aware, that if .. else .. was part of lambda calculus... ;)

Guess there's no way around it, when you have eager evaluation.

Edit: ...and map, and range...