For a person without a comp-sci background, what is a lambda in the world of Computer Science?
相关问题
- Why wrapping a function into a lambda potentially
- Generating powerset in one function, no explicit r
- Check if tie in count of lists using linq
- C# to VB - How do I convert this anonymous method
- applying a list to an entered function to check fo
相关文章
- Why doesn't C11 support lambda functions
- Algorithm for partially filling a polygonal mesh
- Should client-server code be written in one “proje
- Algorithm for maximizing coverage of rectangular a
- Is there an existing solution for these particular
- Will java allow to use functional interfaces as me
- Detect if C++ lambda can be converted to function
- What is Scope Creep? [closed]
The question has been answered fully, I don't want to go into details. I want to share the usage when writing numerical computation in rust.
There is an example of a lambda(anonymous function)
When I was writing a module of Newton–Raphson method, it was used as first and second order derivative. (If you want to know what is Newton–Raphson method, please visit "https://en.wikipedia.org/wiki/Newton%27s_method".
The output as the following
@Brian I use lambdas all the time in C#, in LINQ and non-LINQ operators. Example:
Before C#, I used anonymous functions in JavaScript for callbacks to AJAX functions, before the term Ajax was even coined:
The interesting thing with C#'s lambda syntax, though, is that on their own their type cannot be infered (i.e., you can't type var foo = (x,y) => x * y) but depending on which type they're assigned to, they'll be compiled as delegates or abstract syntax trees representing the expression (which is how LINQ object mappers do their "language-integrated" magic).
Lambdas in LISP can also be passed to a quotation operator and then traversed as a list of lists. Some powerful macros are made this way.
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:
'x', 'y' are names and 1, 'value' are values. Try a function in mathematics
Error reports,
you cannot write a mathematic directly as code,'n' should be defined or be assigned to a value.
It works now,what if you insist on combining the two seperarte lines to one. There comes
lambda
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.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:
It works for simple statements,there's 11 types of them in python 7. Simple statements — Python 3.6.3 documentation
How about compound statement,
There comes
def
enable it workingTada, 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.It's formatting like:
It's not a smart strategy, so error reports
We have to delete one of them,set a function without a name.
It's called 'anonymous function'
In conclusion,
lambda
in an inline function which enable you to write a function in one straight line as does in mathematicslambda
is anonymousHope, this helps.
Lambda comes from the Lambda Calculus and refers to anonymous functions in programming.
Why is this cool? It allows you to write quick throw away functions without naming them. It also provides a nice way to write closures. With that power you can do things like this.
Python
As you can see from the snippet of Python, the function adder takes in an argument x, and returns an anonymous function, or lambda, that takes another argument y. That anonymous function allows you to create functions from functions. This is a simple example, but it should convey the power lambdas and closures have.
Examples in other languages
JavaScript
JavaScript (ES6)
Scheme
C# 3.5 or higher
Swift
PHP
Haskell
Java see this post
Lua
Kotlin
Ruby
Ruby is slightly different in that you cannot call a lambda using the exact same syntax as calling a function, but it still has lambdas.
Ruby being Ruby, there is a shorthand for lambdas, so you can define
adder
this way:The question is formally answered greatly, so I will not try to add more on this.
In very simple, informal words to someone that knows very little or nothing on math or programming, I would explain it as a small "machine" or "box" that takes some input, makes some work and produces some output, has no particular name, but we know where it is and by just this knowledge, we use it.
Practically speaking, for a person that knows what a function is, I would tell them that it is a function that has no name, usually put to a point in memory that can be used just by referencing to that memory (usually via the usage of a variable - if they have heard about the concept of the function pointers, I would use them as a similar concept) - this answer covers the pretty basics (no mention of closures etc) but one can get the point easily.
I have trouble wrapping my head around lambda expressions because I work in Visual FoxPro, which has Macro substitution and the ExecScript{} and Evaluate() functions, which seem to serve much the same purpose.
One definite benefit to using formal lambdas is (I assume) compile-time checking: Fox won't know if you typo the text string above until it tries to run it.
This is also useful for data-driven code: you can store entire routines in memo fields in the database and then just evaluate them at run-time. This lets you tweak part of the application without actually having access to the source. (But that's another topic altogether.)