计划中lambda的重点是什么?

Cam*_*Cam 26 lambda scheme let

我正在学习计划.我知道如何使用lambda和let表达式.

然而,我正在努力弄清楚使用lambda的重点是什么.你不能用lambda做你能做的一切吗?

看一个lambda表达式是比let更好的选择的例子会特别有用.

还有一件事 - 是否还有让let比lambda更有用的情况?如果是这样的例子也会很好.

编辑:我也对对比定义和lambda感兴趣,因为它们似乎执行类似的任务.


更新:

感谢大家的帮助.在阅读了你的答案之后,我做了一些更多的研究lambda/let/define,现在对它的了解要好得多.

我来自一个很酷的lambda用法的好例子 - 从程序中返回匿名函数.例如,operateTwice下面的过程返回一个匿名函数,该函数基于传递给过程的参数:

(define operateTwice
  (lambda (op1 op2)
    (lambda (x y)
      (op2 (op1 x y) y))))

((operateTwice * +) 2 3) ;equivalent to: (+ (* 2 3) 3), or in standard notation 2*3+3
Run Code Online (Sandbox Code Playgroud)

输出:

9
Run Code Online (Sandbox Code Playgroud)

elj*_*nso 44

A let是一个lambda.

例如

(let ((x 1))
  body)
Run Code Online (Sandbox Code Playgroud)

可以翻译成

((lambda (x) body) 1)
Run Code Online (Sandbox Code Playgroud)

此外,在Scheme中,所有控制和环境结构都可以由lambda表达式和lambdas的应用程序表示.

因此,lambda它比letScheme中的许多有趣结构的基础更强大并且形成了它的基础.

关注definelambda,顶级define添加绑定到顶级环境.

当你写作

(define (f x)
  body)
Run Code Online (Sandbox Code Playgroud)

你真的在说

(define f (lambda (x) body))
Run Code Online (Sandbox Code Playgroud)

嵌套定义被翻译成letrec,也可以使用lambdas重写.

所以,再一次,很多Scheme结构都可以被翻译成使用的东西lambda,因此你很清楚它真的很值得lambda.

  • 嘿,你甚至可以用lambda来构建自然数和芳基化操作. (4认同)

sep*_*p2k 14

您可以使用lambda,如果你想创建一个函数来使用它作为参数传递给另一个函数(例如像map),但你实际上并没有想命名的功能.

例如,如果要为列表中的每个数字添加42,则可以执行以下操作:

(define (add42 x) (+ x 42))
(map add42 (list 1 2 3 4))
Run Code Online (Sandbox Code Playgroud)

但是如果你不想给你只使用一次的函数命名,你可以这样做:

(map (lambda (x) (+ x 42)) (list 1 2 3 4))
Run Code Online (Sandbox Code Playgroud)

  • 在方案和其他功能程序中,您使用的功能非常多,即使您只使用它们一次,也不希望通过定义它们来污染您的命名空间.另外,非常简单的函数不能承受单独定义它们的开销,并且在另一个地方寻找它们的定义比在你需要它们的地方更加烦人.一些使用后它会变得明显.没有实际体验它很难解释. (5认同)
  • 为什么只在一个场合需要它时给它一个名字?您可以只传递一个匿名函数,而不是考虑一些不会使命名空间混乱的名称. (3认同)

Ibr*_*mad 5

Let is actually just shorthand for a Lambda expression. The following two expressions are equivalent:

(let ((alpha 7)) (* 5 alpha))

((lambda (alpha) (* 5 alpha)) 7)
Run Code Online (Sandbox Code Playgroud)

Lambda follows the philosophy of the language that everything should look like a mathematical function. But in practice Let makes it easier to figure out what is happening if there are too many variables. Imagine 10 variables which have their values defined after the Lambda block, and you trying to match each of those with the variable name, with Let the values of variables are placed right next to their names, convenient for the programmer but conforming less to the Functional Programming philosophy.

Lambda can be used to return a function from a higher order function however let cannot do that. Eg:

(define (plus-list x)
  (cond ((number? x)
         (lambda (y) (+ (sum-n x) y)))
        ((list? x)
         (lambda (y) (+ (sum-list x) y)))
        (else (lambda (x) x))
        ))

> ((plus-list 3) 4)
10
> ((plus-list '(1 3 5)) 5)
14
> ((plus-list 'a) 5)
5
Run Code Online (Sandbox Code Playgroud)

Lambda can also be used to pass a function to a function:

>(map (lambda (x) (+ 1 x)) '(-1 2 -3))
(0 3 -2)
Run Code Online (Sandbox Code Playgroud)