方案拉姆达?它是什么?

Max*_*eld 0 scheme racket mit-scheme

方案中是什么?我们如何使用它?

scm> (define (x) 100)
x
scm> (x)
100
scm> x ; When we "called" x, it return (lambda () 100). what is it ?
(lambda () 100)
Run Code Online (Sandbox Code Playgroud)

Syl*_*ter 5

(define (x) 100)是相同的:

(define x          ; define the variable named x
        (lambda () ; as a anoymous function with zero arguments
          100))    ; that returns 100

x   ; ==> #<function> (some representation of the evaluated lambda object, there is no standard way)
(x) ; ==> 100 (The result of calling the function)
Run Code Online (Sandbox Code Playgroud)

您可能更喜欢 Algol 语言,因此 JavaScript 中也是如此:

function x () { return 100; }是相同的:

var x =          // define the variable named x
  function () {  // as the anonymous function with zero arguments
    return 100;  // that returns 100
  };
x;   // => function () { return 100; } (prints its source)
x(); // => 100 (the result of calling the function)
Run Code Online (Sandbox Code Playgroud)

初学者有时会在变量周围添加括号,例如((x))和 ,这相当于x()()用 Algol 语言编写。因此x必须是一个零参数的函数,它将返回一个零参数的函数才能工作。