为什么《小阴谋家》中的所有 lambda 表达式都是如此?
在从 SICP 学习了一些计划之后,我开始阅读《小计划》(我觉得这本书很有趣),并且已经完成了大约四分之一。我注意到我可以在不使用 lambda 的情况下编写许多(大多数?全部?)解决方案,而 The Little Scheduler总是使用它们。例如,
(define atom?
(lambda (x)
(and (not (pair? x)) (not (null? x)))))
除非我弄错了,否则第一个定义可以写得更简单,因为
(define (atom? x)
(and (not (pair? x)) (not (null? x))))
如果我编写无 lambda 解决方案,我会错过一些基本的东西吗?
After learning a bit of Scheme from SICP, I started reading The Little Schemer (which I find quite entertaining) and am about one fourth done. I noticed that I can write many (most? all?) solutions without using lambda whereas The Little Schemer always uses them. For example, the very first definition is
(define atom?
(lambda (x)
(and (not (pair? x)) (not (null? x)))))
which, unless I am mistaken, can be written more simply as
(define (atom? x)
(and (not (pair? x)) (not (null? x))))
Am I missing something fundamental if I write lambda-less solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我非常喜欢 lambda 风格的教学,因为正如 Jay 所说,它使函数创建更加明确。
学习时,您从
atom?
等开始的简单函数是在顶层定义
的。这意味着可以使用defun
< 创建函数,甚至更加紧凑/a>-styledefine
你提到的。但是,当您开始使用函数作为一等值时,例如,作为
map
,您将第一次看到lambda
,它可能看起来比实际情况更奇怪、更神奇。相反,如果您一直使用 lambda 定义函数,那么就可以轻松看出函数与任何其他值一样。它们经常出现在
define
的右侧,但与数字或带引号的常量没有什么不同:当然,该语言支持这两种形式,因此最终取决于样式。对我来说,当所有函数都使用 lambda 构建时,define 的使用具有吸引人的一致性:第一个参数始终是符号,第二个参数是只是任何旧的表达方式。事实上,
lambda
就像任何旧的表达式一样,是任何函数式程序员需要学习的最重要的事情之一。I strongly prefer the
lambda
-heavy style for teaching, since it makes function creation more explicit, as Jay says.When learning, the simple functions you start with like
atom?
aredefine
d at the top level. This means it's possible, and even more compact, to create the function with thedefun
-styledefine
you mention.However, when you start using functions as first-class values, e.g., as an argument to
map
, you'll be seeinglambda
for the first time, and it might seem weirder and more magical than it really is.Instead, if you've been defining your functions with
lambda
the whole time, it's less of a leap to see that functions are just like any other value. They happen to be on the right-hand side ofdefine
pretty frequently, but are no different from a number or a quoted constant:Of course, the language supports both forms, so it comes down to style eventually. To me, there is an appealing consistency in the usage of
define
when all of your functions are made withlambda
: the first argument is always a symbol, and the second argument is just any old expression. And the fact thatlambda
is just like any old expression is one of the most important things for any functional programmer to learn.最初,
define
使用单一语法来将变量设置为值。这就是这些古老(且永恒)书籍中使用的风格。后来,define
获得了一种不同的语法作为快捷方式,这就是您正在使用的语法。只是为了好玩,搜索您的方案库,您可能会发现一个宏,它将非 lambda 形式扩展为旧的 lambda 重形式。
Originally,
define
had a single syntax, to set a variable to a value. That's the style used in such old (and timeless) books. Later on,define
got a different syntax as a shortcut, which is the one you're using.Just for fun, search on your Scheme libraries, you might find a macro that expands the non-lambda form into the old lambda-heavy one.
您可以使用
expand
(如果支持)查看您的Scheme将这些快捷方式(宏)扩展为什么:mzscheme 4.2.4(使用DrScheme):
ChezScheme 8.0:
出现
lambda
平淡无奇。You can see what your Scheme expands these shortcuts (macros) into using
expand
(if supported):mzscheme 4.2.4 (with DrScheme):
Chez Scheme 8.0:
The
lambda
appears plain as day.我依稀记得一位教授讨论过这样的事情。
我认为使用 lambda 解决方案有两个原因:
第一个纯粹是历史事件。在某个时间点,这是唯一可能的方法。所以有些人仍然使用这种方法。
第二个是,有些人只是喜欢更明确地说明正在创建函数的事实,因此他们喜欢看到 lambda 这个词。
所以我相信选择取决于你个人最喜欢的。
I vaguely remember a professor discussing something like this.
I think the lambda solution is used for two reasons:
The first is purely a historical thing. At one point in time, that was the only way it was possible. So some people still use that method.
The second is that some people just like to be more explicit about the fact that a function is being created, so they like to see the word lambda.
So I believe the choice comes down to what ever you personally like the best.
当我使用 TLS 时,我正在阅读一些有关 lambda 演算的内容(阅读 Simon Peyton Jones 所著的《函数式编程语言的实现》;免费在线 pdf)。所以这只是一个猜测,但我相信 TLS 的作者希望您在思考中真正重视 lambda。他们没有出来说出来,但有暗示(查看 TLS 第 107 页)这只是应用 lambda 计算的练习。所以也许他们只是说而不说,“你正在做 lambda 抽象,我的朋友!”
I'm reading a bit about lambda calculus (reading "The Implementation of Functional Programming Languages" by Simon Peyton Jones; free pdf on-line) as I use TLS. And so this is just a guess, but I believe the authors of TLS want you to really be lambda-heavy in your thinking. They don't come out and say it, but there are hints (check out p. 107 of TLS) that this is all just an exercise in applied lambda calc. So maybe they're saying without saying, "You're doing lambda abstractions, my friend!"
小计划者使用伪代码计划(出于教育目的进行简化并独立于实现)。今天的标准方案有一个定义的定义,您可以在其中隐式调用 lambda(请参阅 http://www.cs.cmu.edu/Groups/AI/html/r4rs/r4rs_7.html)。 Little Schemer 方案非常简单,不包括这种替代形式。
The Little Schemer uses a pseudo-code Scheme (to make simplifications for educational purposes and to be implementation-independent). Today's standard Scheme has a definition of define in which you are implicitly invoking lambda (see http://www.cs.cmu.edu/Groups/AI/html/r4rs/r4rs_7.html). The Little Schemer scheme is very simple and does not include this alternate form.