In-depth analysis of the usage of lambda expressions in Python, pythonlambda
Ordinary mathematical operations are defined by this pure abstract symbolic calculus, and the calculation results can only exist in the mind. So I wrote some code to verify the algorithm rules described in this article.
Let's verify the natural numbers and calculation rules described in this article. Speaking of natural numbers, Baidu also gave it a bit today. According to du Niang, after 1993, the State stipulated that 0 is a natural number. First, define the calculation rules of natural numbers and natural numbers:
Use lambda expressions to define a natural number (Qiu Qi number)
0 := λf.λx.x1 := λf.λx.f x2 := λf.λx.f (f x)3 := λf.λx.f (f (f x))...
The preceding definition intuitively refers to the number n, which is the n-order function of f (x. 1 is f (x), 2 is f (x)... strictly speaking, this statement is not accurate. In fact, each Qiu odd number is a second-order function, which has two variables f and x. The binary naming function is used to express the following:
0 -> num0(f,x)=x1 -> num1(f, x)=f(x)2 -> num2(f,x)=f(f(x))3 -> num3(f,x)=f(f(f(x)))...
The parameter f is a function. This section is a bit difficult to understand, but it cannot be understood.
First, use recursion to define Qiu Qi number (natural number)
0 is a natural number. Du Niang said that 0 is a natural number after 1993.
Each natural number has a future.
Code expression:
NUM0=lambda f: lambda x:xSUCC=lambda n: lambda f: lambda x: f(n(f)(x))
The following operators are defined, including addition, multiplication, subtraction, and power. The Wikipedia does not describe division. It is estimated that Division definition is complicated and cannot be clearly stated at the moment. Then we will not verify it.
#################################################define number calculus rules################################################ #define Church numeral inductively.#0 := λf.λx.x#1 := λf.λx.f x#2 := λf.λx.f (f x)#3 := λf.λx.f (f (f x))#...NUM0=lambda f: lambda x:xSUCC=lambda n: lambda f: lambda x: f(n(f)(x)) #define OperatorPLUS=lambda m: lambda n: m(SUCC)(n)MULT= lambda m: lambda n: m(PLUS(n))(NUM0)#define predecessor to obtain the previous number.PRED= lambda n: lambda f: lambda x: n(lambda g: lambda h: h(g(f)))(lambda u:x)(lambda u:u)SUB=lambda m: lambda n: n(PRED)(m)POW=lambda b: lambda e: e(b)
Defines the operations of natural numbers and natural numbers. Then the calculation of natural numbers can be calculated using lambda.
The problem is that the above definition is an abstract symbolic algorithm. We need an encoder to encode the abstract chunch numeral symbol above into a readable form, the number entered by a person must also be decoded into an abstract symbol.
#################################################create encoder to input/output Church numeral################################################ class LambdaEncoding: @staticmethod def encoding(exp,encoder): return encoder().encoding(exp) @staticmethod def decoding(s, decoder): return decoder().decoding(s) class NumEncoder: def encoding(self,num): f=lambda x:x+1 return str(num(f)(0)) def decoding(self,s): n=int(s) num=NUM0 for i in range(n): num=SUCC(num) return num
Well, with the encoder, you can easily verify it.
#################################################calculus demo################################################print("demo number calculus.\n" "don't input large number," "it will cause to exceed maximum recursion depth!\n") n1=input('input a number: ')n2=input('input anohter number: ')#decode string to Church numeralnum1=LambdaEncoding.decoding(n1,NumEncoder)num2=LambdaEncoding.decoding(n2,NumEncoder) #addresult=PLUS(num1)(num2) print('{0} + {1} = {2}'.format( n1, n2, LambdaEncoding.encoding(result, NumEncoder))) #multresult=MULT(num1)(num2)print('{0} X {1} = {2}'.format( n1, n2, LambdaEncoding.encoding(result, NumEncoder)))#subresult=SUB(num1)(num2)print('{0} - {1} = {2}'.format( n1, n2, LambdaEncoding.encoding(result, NumEncoder))) #POWresult=POW(num1)(num2)print('{0} ^ {1} = {2}'.format( n1, n2, LambdaEncoding.encoding(result, NumEncoder)))
The test results are as follows:
>>> demo number calculus.don't input large number,it will cause to exceed maximum recursion depth! input a number: 4input anohter number: 34 + 3 = 74 X 3 = 124 - 3 = 14 ^ 3 = 64>>>
Amazing.
Differences between lambda and def
Python lambda uses lambda in python to create anonymous functions, while the method created with def has a name, except that the method names on the surface are different, what are the differences between python lambda and def?
1. python lambda creates a function object, but does not assign this function object to an identifier. def assigns the function object to a variable.
2. python lambda is just an expression, while def is a statement.
The following is a simplified python lambda format.
lambda x: print x
If you use python lambda in python list parsing, it doesn't make much sense, because python lambda creates a function object, but it is discarded immediately, because you didn't use its return value, that is, the function object. Lambda is just an expression that can be directly used as a member of the python list or python dictionary, for example:
info = [lamba a: a**3, lambda b: b**3]
There is no way to directly replace it with the def statement. Because def is a statement, not an expression that cannot be nested in it, lambda expressions can only have one expression after. That is to say, in def, return can be returned or placed behind lambda. return cannot be used or defined behind python lambda. Therefore, statements such as if, for, or print cannot be used in lambda. lambda is generally only used to define simple functions.
Here are some examples of python lambda.
1. For a single parameter:
g = lambda x:x*2print g(3)
The result is 6.
For multiple parameters:
m = lambda x,y,z: (x-y)*zprint m(3,1,2)
Result 4