Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

added 4 characters in body
Source Link
marc_s
  • 760.3k
  • 186
  • 1.4k
  • 1.5k

No errors report,
This'sreported.

This is a glance at lambda, it enables you to write a function in a single line as you do in mathematic into the computer directly.

As illustrated above,
the the equals symbol = works for simple data(1 and 'value') type and simple expression(n**2 + 2*n + 1).

Try this,:

How about compoudedcompound statement,

Tada,
Analyse analyse it, 'm' is name, 'n**2 + 2*n + 1' is value.: is a variant of '='.
Find it, if just for understanding, everything starts from assignment and everything is assignment.

Now return to lambda,

We we have a function named 'm'

Try,:

There'sThere are two names of 'm' here,function function m already has a name, duplicated.

  1. lambadalambda in an inline function which enable you to write a funtionfunction in one straight line as does in mathematics
  2. lambadalambda is anonymous

No errors report,
This's a glance at lambda, it enables you to write a function in a single line as you do in mathematic into the computer directly.

As illustrated above,
the equals symbol = works for simple data(1 and 'value') type and simple expression(n**2 + 2*n + 1).

Try this,

How about compouded statement,

Tada,
Analyse it, 'm' is name, 'n**2 + 2*n + 1' is value.: is a variant of '='.
Find it, if just for understanding, everything starts from assignment and everything is assignment.

Now return to lambda,

We have a function named 'm'

Try,

There's two names of 'm' here,function m already has a name, duplicated.

  1. lambada in an inline function which enable you to write a funtion in one straight line as does in mathematics
  2. lambada is anonymous

No errors reported.

This is a glance at lambda, it enables you to write a function in a single line as you do in mathematic into the computer directly.

As illustrated above, the equals symbol = works for simple data(1 and 'value') type and simple expression(n**2 + 2*n + 1).

Try this:

How about compound statement,

Tada, analyse it, 'm' is name, 'n**2 + 2*n + 1' is value.: is a variant of '='.
Find it, if just for understanding, everything starts from assignment and everything is assignment.

Now return to lambda, we have a function named 'm'

Try:

There are two names of 'm' here, function m already has a name, duplicated.

  1. lambda in an inline function which enable you to write a function in one straight line as does in mathematics
  2. lambda is anonymous
added 37 characters in body
Source Link
Wizard
  • 22.7k
  • 22
  • 96
  • 162

Error reports, you
you cannot write a mathematic directly as code,'n' should be defined or be assigned to a value.

No errors report,
This's a glance at lambda, it enables you to write a siglefunction in a single line ofas you do in mathematic function into the computer directly. We

We will see it later.

As illustrated above, the
the equals symbol = works for simple data(1 and 'value') type and simple expression(n**2 + 2*n + 1).

Tada,
Analyse it, 'm' is name, 'n**2 + 2*n + 1' is value.: is a variant of '='.
Find it, if just for understanding, everything starts from assignment and everything is assignment.

Error reports, you cannot write a mathematic directly as code,'n' should be defined or be assigned to a value.

No errors report This's a glance at lambda, it enables you to write a sigle line of mathematic function into computer directly. We will see it later.

As illustrated above, the equals symbol = works for simple data(1 and 'value') type and simple expression(n**2 + 2*n + 1).

Tada, Analyse it, 'm' is name, 'n**2 + 2*n + 1' is value.: is a variant of '='. Find it, if just for understanding, everything starts from assignment and everything is assignment.

Error reports,
you cannot write a mathematic directly as code,'n' should be defined or be assigned to a value.

No errors report,
This's a glance at lambda, it enables you to write a function in a single line as you do in mathematic into the computer directly.

We will see it later.

As illustrated above,
the equals symbol = works for simple data(1 and 'value') type and simple expression(n**2 + 2*n + 1).

Tada,
Analyse it, 'm' is name, 'n**2 + 2*n + 1' is value.: is a variant of '='.
Find it, if just for understanding, everything starts from assignment and everything is assignment.

Source Link
Wizard
  • 22.7k
  • 22
  • 96
  • 162

For a person without a comp-sci background, what is a lambda in the world of Computer Science?

I will illustrate it intuitively step by step in simple and readable python codes.

In short, a lambda is just an anonymous and inline function.

Let's start from assignment to understand lambdas as a freshman with background of basic arithmetic.

The blueprint of assignment is 'the name = value', see:

In [1]: x = 1
 ...: y = 'value'
In [2]: x
Out[2]: 1
In [3]: y
Out[3]: 'value'

'x', 'y' are names and 1, 'value' are values. Try a function in mathematics

In [4]: m = n**2 + 2*n + 1
NameError: name 'n' is not defined

Error reports, you cannot write a mathematic directly as code,'n' should be defined or be assigned to a value.

In [8]: n = 3.14
In [9]: m = n**2 + 2*n + 1
In [10]: m
Out[10]: 17.1396

It works now,what if you insist on combining the two seperarte lines to one. There comes lambda

In [13]: j = lambda i: i**2 + 2*i + 1
In [14]: j
Out[14]: <function __main__.<lambda>>

No errors report This's a glance at lambda, it enables you to write a sigle line of mathematic function into computer directly. We will see it later.

Let's continue on digging deeper on 'assignment'.

As illustrated above, the equals symbol = works for simple data(1 and 'value') type and simple expression(n**2 + 2*n + 1).

Try this,

In [15]: x = print('This is a x')
This is a x
In [16]: x
In [17]: x = input('Enter a x: ')
Enter a x: x

It works for simple statements,there's 11 types of them in python 7. Simple statements — Python 3.6.3 documentation

How about compouded statement,

In [18]: m = n**2 + 2*n + 1 if n > 0
SyntaxError: invalid syntax
#or
In [19]: m = n**2 + 2*n + 1, if n > 0
SyntaxError: invalid syntax

There comes def enable it working

In [23]: def m(n):
 ...: if n > 0:
 ...: return n**2 + 2*n + 1
 ...:
In [24]: m(2)
Out[24]: 9

Tada, Analyse it, 'm' is name, 'n**2 + 2*n + 1' is value.: is a variant of '='. Find it, if just for understanding, everything starts from assignment and everything is assignment.

Now return to lambda,

We have a function named 'm'

Try,

In [28]: m = m(3)
In [29]: m
Out[29]: 16

There's two names of 'm' here,function m already has a name, duplicated.

It's formatting like:

In [27]: m = def m(n):
 ...: if n > 0:
 ...: return n**2 + 2*n + 1
 SyntaxError: invalid syntax

It's not a smart strategy, so error reports

We have to delete one of them,set a function without a name.

m = lambda n:n**2 + 2*n + 1

It's called 'anonymous function'

In conclusion,

  1. lambada in an inline function which enable you to write a funtion in one straight line as does in mathematics
  2. lambada is anonymous

Hope, this helps.

AltStyle によって変換されたページ (->オリジナル) /