简体   繁体   English

使用lambda表达式在Scheme中定义函数的原因是什么?

[英]What's the reason for using lambda expressions to define functions in Scheme?

I am reading the The Little Schemer book by Daniel Friedman. 我正在阅读Daniel Friedman撰写的The Little Schemer一书。 I don't understand why every function in the book is written with a lambda expression. 我不明白为什么书中的每个函数都是用lambda表达式编写的。

(define fun1
  (lambda (x1 x2)
    (+ x1 x2)))

(define (fun2 x1 x2)
  (+ x2 x2))

The two functions do the same thing, but the second one is neater. 这两个功能做同样的事情,但第二个功能更整洁。 I am curious about the reasons to use lambda everywhere. 我很好奇在各地使用lambda的原因。

I used DrRacket in this case. 在这种情况下我使用了DrRacket。

(define (x . a) ...) 

is just an abbreviation for 只是一个缩写

(define x (lambda a ...))

In the very first scheme report from 1975 you didn't have this abbreviation. 在1975年的第一个方案报告中,你没有这个缩写。 In the revised report from 1978 it was introduced. 在1978年的修订报告中,它被引入。

The little Schemer is just a later edition of The little Lisper from 1974. It predates Scheme and when fixed to follow Scheme they tried to keep is as close to the original as possible. 这个小小的Schemer只是1974年的The Little Lisper的后期版本。它早于Scheme,当它被修复为遵循Scheme时,他们试图保持尽可能接近原始版本。 Besides when you use (define x (lambda (arg1) ...)) it's obvious that procedure bindings are not different from other variable bindings other than the object it points to is closure code rather than data. 此外,当你使用(define x (lambda (arg1) ...)) ,很明显,过程绑定与其他变量绑定没有区别,除了它指向的对象是闭包代码而不是数据。

If you look at the SICP video lectures you'll see that Abelson and Sussman do have students who are confused with this so it's probably best using only one of these and since anonymous procedures are something you need to touch eventually it's obvious you want to teach the form with a explicit lambda instead of the syntactic sugar. 如果你看看SICP视频讲座,你会发现Abelson和Sussman确实有学生对此感到困惑,所以最好只使用其中一个,因为匿名程序是你需要触摸的东西,很明显你想教带有显式lambda而不是语法糖的形式。

It's simply a matter of style. 这只是一种风格问题。 In The Little Schemer the authors decided to explicitly declare procedures using lambdas, to make it clear that a function definition assigns a name to a procedure (which is just an expression with a value, like any other). 在The Little Schemer中,作者决定使用lambdas显式声明过程,以明确函数定义为过程赋予一个名称(它只是一个带有值的表达式,就像任何其他过程一样)。

The other way to declare functions (without explicit lambdas) is completely equivalent, and it's just syntactic sugar. 声明函数(没有显式lambdas)的另一种方式是完全等效的,它只是语法糖。 One could argue that the authors chose the more explicit syntax for pedagogical, not practical reasons. 有人可能会争辩说,作者为教学而非实际原因选择了更明确的语法。

A lambda expression is a function literal, just like 3 (for example) is an integer literal. lambda表达式是一个函数文字,就像3(例如)是一个整数文字。 You could imagine a programming language where you couldn't use 3 in a larger expression, but had to define a variable and initialize it to 3. It would still work, but it would be inconvenient and get in the way. 你可以想象一种编程语言,你不能在一个更大的表达式中使用3,但必须定义一个变量并将其初始化为3.它仍然可以工作,但它会带来不便并妨碍你。 Most languages are like that with functions (they can just be defined once and then called), but functional languages let you treat functions like any other variable, including being able to refer to them literally. 大多数语言都与函数一样(它们只能被定义一次,然后被调用),但是函数式语言可以让你像处理任何其他变量一样处理函数,包括能够从字面上引用它们。

Just like you use integer literal when you need a specific integer but don't need to bother with a variable, you use a lambda expression when you need a function but don't need to bother naming it. 就像你需要一个特定的整数但不需要打扰变量时使用整数文字一样,当你需要一个函数但不需要打扰它时,你可以使用一个lambda表达式。

A common use is with functions like mapcar which takes a function as an argument and applies that function to all the elements in the other argument. 常见的用途是使用mapcar这样的函数,它将函数作为参数并将该函数应用于另一个参数中的所有元素。 Sometimes you just want to throw a one-off function into that without needing to name it. 有时您只想将一次性功能抛入其中而无需命名。

Just looking at that particular example, I would guess they're pointing out a difference between scheme and lisp (lisp does that somewhat differently). 只看这个特定的例子,我猜他们会指出scheme和lisp之间的区别(lisp做的有点不同)。

I'm just finishing The Little LISPer myself. 我自己刚刚完成The Little LISPer Initially, I was confused as to why they had employed this strange idiom. 最初,我很困惑为什么他们使用这个奇怪的习语。 As you say, the latter is sugar for the former, and certainly looks neater, so what's the point in using anonymous functions everywhere? 正如你所说,后者是前者的糖,当然看起来更整洁,那么到处使用匿名函数有什么意义呢?

From a pedagogic point of view, I think that it prepares the reader better for the final chapters, which cover currying, higher-order functions, the lambda calculus and so on. 从教育学的角度来看,我认为它为读者做好了准备,最后的章节涵盖了currying,高阶函数,lambda演算等等。 In particular, the derivation of the applicative-order Y-combinator requires the use of what you might call "unnecessary function-wrapping" in other contexts. 特别是,应用顺序Y组合子的推导需要在其他环境中使用你可能称之为“不必要的函数包装”的东西。 By the penultimate chapter, you're so familiar with this procedure that deriving the Y-combinator is no more difficult than pouring a coffee or making some toast. 通过倒数第二章,你对这个程序非常熟悉,推出Y-combinator并不比倒咖啡或做吐司更困难。 Okay--not really, but I think that the style adopted by the authors certainly helps bring you closer to understanding this difficult idea. 好吧 - 不是真的,但我认为作者采用的风格无疑会让你更接近理解这个难题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM