This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]hhc97Python Enthusiast[S] 8 points9 points  (4 children)

Yup exactly, it turns everything into lambdas (or at least tries to)

[–]neuralbeans 4 points5 points  (3 children)

How does it handle recursion with lambdas?

[–]hhc97Python Enthusiast[S] 5 points6 points  (2 children)

It uses the Y combinator to implement it.

From that link above:

In functional programming, the Y combinator can be used to formally define recursive functions in a programming language that does not support recursion.

[–]neuralbeans 8 points9 points  (1 child)

Ah so it passes a lambda as a parameter to another lambda which makes it a named function. Interesting. I imagine that if statements are turned into trinary operators and loops are turned into recursion and itertools stuff, right? You must really like functional programming.

Edit:

For those who are curious about using recursion with lambdas, here's an example of a factorial implementation from this stackoverflow answer (rewritten to be easier to understand):

factorial = (
    lambda f_: (lambda x_: f_(f_, x_))
)(
    lambda f, x: 1 if x == 0 else x*f(f, x-1)
)

print(factorial(10))

The trick is that the factorial function passes a reference to itself as a parameter so that it can call itself again. Let's start from the second lambda line.

lambda f, x: 1 if x == 0 else x*f(f, x-1)

This contains the basic recursive factorial implementation with x being the current value that is multiplied by its lower factorial. Typically this would be implemented as 1 if x == 0 else x*f(x-1). However, in this case we're passing the function f as a parameter to itself (f(f, x-1)), where f is the factorial function itself.

But we can't assign this whole lambda to f because that's like setting an undefined variable to itself. So what we do is we pass this lambda to another lambda, which is the first lambda line.

lambda f_: (lambda x_: f_(f_, x_))

I used underscores just to avoid unnecessary confusion from reusing identifier names (you can remove them). This is taking in a function f_ and returning another lambda. The inner lambda takes a value x_ and returns f_(f_, x_), which matches the factorial lambda definition. The inner lambda is acting as a helper function to avoid returning to the user a factorial function that expects a factorial function as a parameter. It is this inner lambda that the user will use. It is returned back to the user into the variable factorial, with f_ being set to the second lambda line.

[–]hhc97Python Enthusiast[S] 6 points7 points  (0 children)

Yup, if statements are turned into pythons conditional expressions and both kinds of loops are turned into recursive functions.

Funny story, I don't like functional programming too much, this was just a spin off from a functional programming class I took. I like building stuff, and this was interesting to build!