I was able to recurse in Python: lambda

Referenced article: Comments on [JavaScript, VB.Net, Python3, Common Lisp, Clojure, HSP, R language] to suppress side effects and substitutions with Fizz Buzz problem / 543eed9386103ec9c58c # comment-7f730579d0e5e1267525) One liner in Python: recursion You don't have to make a complicated Y combinator, you just need the function to have an argument to receive itself.

I thought I couldn't write recursion in lambda (unless I did something special), but I can. I did not know that. No, I think I used to go through my brain because I couldn't understand it even if I read it.

I wrote the factorial myself by referring to the article.

do = pack_to_tuple = lambda *x : x
case = unpack_and_evaluate_in_order_then_return_last = lambda x : x[-1]
otherwise = True

f = ( lambda x :
        case(     x == 0   and do( 1 )
              or otherwise and do( x * f( x - 1 ) )
             )
    )

f2 = ( lambda x :
       ( lambda f : f( f,  x ) ) ( lambda f, x :
                                     case(     x == 0   and do( 1 )
                                           or otherwise and do( x * f(f,  x - 1 ) )
                                         )
                                 )
     )

print(f(10), f2(10))

python


#result
3628800 3628800

It does the same thing as the upper function `f``` and the lower function f2 ``. The upper one is recurring normally. I usually do this and understand. What about the bottom? It's a little difficult ... Do you feel like you're creating a function that returns factorials with anonymous recursion and naming it f2?

In the argument f of an anonymous function that takes + x and f as arguments and returns f (f, x), Pass an anonymous function that takes + f and x as arguments and returns 1 or x * f (f, x-1) depending on the value of x Anonymous function with + x as an argument

I don't know what it is ... but it can be mechanically converted from top to bottom, so it's quite so when you need it.

Please comment if it's different or something like this.

Postscript Is this all right?

Reference article: [Python] Recursion with lambda

With keyword arguments ...

python


f3 = ( lambda x, f =  lambda f, x : case(     x == 0   and do( 1 )
                                		or otherwise and do( x * f(f,  x - 1 ) )
                            		)
       : f( f,  x ) 
     )

Clean. Somehow the meaning is easy to understand. I learned a lot.

Recommended Posts

I was able to recurse in Python: lambda
I was able to repeat it in Python: lambda
I was addicted to scraping with Selenium (+ Python) in 2020
I want to be able to run Python in VS Code
I tried to implement PLSA in Python
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
I tried to implement ADALINE in Python
I wanted to solve ABC159 in Python
I tried to implement PPO in Python
What I was addicted to Python autorun
What I was addicted to with json.dumps in Python base64 encoding
[Python / AWS Lambda layers] I want to reuse only module in AWS Lambda Layers
I was addicted to confusing class variables and instance variables in Python
I was able to print the thermal printer "PAPERANG" from Python (Windows10, Python3.6)
I want to do Dunnett's test in Python
I want to create a window in Python
I wrote "Introduction to Effect Verification" in Python
I want to merge nested dicts in Python
I tried to implement TOPIC MODEL in Python
The file name was bad in Python and I was addicted to import
[Python] I was addicted to not saving internal variables of lambda expressions
I tried to implement selection sort in python
I want to display the progress in Python!
I was able to mock AWS-Batch with python, moto, so I will leave it
Summary of points I was addicted to running Selenium on AWS Lambda (python)
I want to write in Python! (1) Code format check
I tried to graph the packages installed in Python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to write in Python! (2) Let's write a test
Even in JavaScript, I want to see Python `range ()`!
I tried to implement a pseudo pachislot in Python
I want to randomly sample a file in Python
I tried to implement Dragon Quest poker in Python
I want to work with a robot in python.
I tried to implement GA (genetic algorithm) in Python
I want to write in Python! (3) Utilize the mock
I want to AWS Lambda with Python on Mac!
I tried to summarize how to use pandas in python
I was addicted to trying logging.getLogger in Flask 1.1.x
I want to use the R dataset in python
What I was addicted to when using Python tornado
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
[Python] Scraping in AWS Lambda
Love was born in Python 2
Login to website in Python
Speech to speech in python [text to speech]
How to develop in Python
I understand Python in Japanese!
What I learned in Python
Love was born in Python 3
How to use Python lambda
Post to Slack in Python
I want to be able to analyze data with Python (Part 3)
I tried to create API list.csv in Python from swagger.yaml
I was able to implement web app authentication with flask-login
I tried to implement a one-dimensional cellular automaton in Python
How to import Python library set up in EFS to Lambda
I want to be able to analyze data with Python (Part 1)