什么是 lambda(函数)? [英] What is a lambda (function)?
问题描述
对于没有 comp-sci 背景的人来说,计算机科学领域中的 lambda 是什么?
For a person without a comp-sci background, what is a lambda in the world of Computer Science?
推荐答案
Lambda 来自 Lambda Calculus 指的是编程中的匿名函数.
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
def adder(x):
return lambda y: x + y
add5 = adder(5)
add5(1)
6
正如您从 Python 代码段中看到的,函数加法器接受一个参数 x,并返回一个匿名函数或 lambda,它接受另一个参数 y.该匿名函数允许您从函数创建函数.这是一个简单的例子,但它应该传达了 lambda 和闭包的强大功能.
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.
其他语言的示例
Perl 5
sub adder {
my ($x) = @_;
return sub {
my ($y) = @_;
$x + $y
}
}
my $add5 = adder(5);
print &$add5(1) == 6 ? "ok
" : "not ok
";
JavaScript
var adder = function (x) {
return function (y) {
return x + y;
};
};
add5 = adder(5);
add5(1) == 6
JavaScript (ES6)
const adder = x => y => x + y;
add5 = adder(5);
add5(1) == 6
方案
(define adder
(lambda (x)
(lambda (y)
(+ x y))))
(define add5
(adder 5))
(add5 1)
6
Func<int, Func<int, int>> adder =
(int x) => (int y) => x + y; // `int` declarations optional
Func<int, int> add5 = adder(5);
var add6 = adder(6); // Using implicit typing
Debug.Assert(add5(1) == 6);
Debug.Assert(add6(-1) == 5);
// Closure example
int yEnclosed = 1;
Func<int, int> addWithClosure =
(x) => x + yEnclosed;
Debug.Assert(addWithClosure(2) == 3);
迅捷
func adder(x: Int) -> (Int) -> Int{
return { y in x + y }
}
let add5 = adder(5)
add5(1)
6
PHP
$a = 1;
$b = 2;
$lambda = fn () => $a + $b;
echo $lambda();
Haskell
(x y -> x + y)
Java 参见这篇文章一个>
// The following is an example of Predicate :
// a functional interface that takes an argument
// and returns a boolean primitive type.
Predicate<Integer> pred = x -> x % 2 == 0; // Tests if the parameter is even.
boolean result = pred.test(4); // true
Lua
adder = function(x)
return function(y)
return x + y
end
end
add5 = adder(5)
add5(1) == 6 -- true
科特林
val pred = { x: Int -> x % 2 == 0 }
val result = pred(4) // true
红宝石
Ruby 略有不同,因为您不能使用与调用函数完全相同的语法来调用 lambda,但它仍然具有 lambda.
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.
def adder(x)
lambda { |y| x + y }
end
add5 = adder(5)
add5[1] == 6
Ruby 是 Ruby,有一个 lambdas 的简写,所以你可以这样定义 adder
:
Ruby being Ruby, there is a shorthand for lambdas, so you can define adder
this way:
def adder(x)
-> y { x + y }
end
R
adder <- function(x) {
function(y) x + y
}
add5 <- adder(5)
add5(1)
#> [1] 6
这篇关于什么是 lambda(函数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!