为什么《小阴谋家》中的所有 lambda 表达式都是如此?

发布于 2024-10-14 08:24:25 字数 410 浏览 7 评论 0原文

在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我只土不豪 2024-10-21 08:24:25

我非常喜欢 lambda 风格的教学,因为正如 Jay 所说,它使函数创建更加明确。

学习时,您从 atom? 等开始的简单函数是在顶层定义的。这意味着可以使用 defun< 创建函数,甚至更加紧凑/a>-style define 你提到的。

但是,当您开始使用函数作为一等值时,例如,作为 map,您将第一次看到 lambda,它可能看起来比实际情况更奇怪、更神奇。

相反,如果您一直使用 lambda 定义函数,那么就可以轻松看出函数与任何其他值一样。它们经常出现在 define 的右侧,但与数字或带引号的常量没有什么不同:

(define x 1)
(define l '(2 3 4 5))
(define s (cons x ls))
(define f (lambda (n) (+ n 2)))

当然,该语言支持这两种形式,因此最终取决于样式。对我来说,当所有函数都使用 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? are defined at the top level. This means it's possible, and even more compact, to create the function with the defun-style define you mention.

However, when you start using functions as first-class values, e.g., as an argument to map, you'll be seeing lambda 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 of define pretty frequently, but are no different from a number or a quoted constant:

(define x 1)
(define l '(2 3 4 5))
(define s (cons x ls))
(define f (lambda (n) (+ n 2)))

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 with lambda: the first argument is always a symbol, and the second argument is just any old expression. And the fact that lambda is just like any old expression is one of the most important things for any functional programmer to learn.

焚却相思 2024-10-21 08:24:25

最初,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.

我的鱼塘能养鲲 2024-10-21 08:24:25

您可以使用expand(如果支持)查看您的Scheme将这些快捷方式(宏)扩展为什么:

mzscheme 4.2.4(使用DrScheme):

> (expand '(define (add1 x) (+ 1 x)))
#<syntax (define-values (add1) (lambda...>
(define-values
  (add1)
  (lambda (x) (apply + '1 x)))

ChezScheme 8.0:

> (expand '(define (add1 x) (+ 1 x)))
(begin
  (set! add1
    (lambda (x)
      (+ 1 x)))
  (void))

出现lambda平淡无奇。

You can see what your Scheme expands these shortcuts (macros) into using expand (if supported):

mzscheme 4.2.4 (with DrScheme):

> (expand '(define (add1 x) (+ 1 x)))
#<syntax (define-values (add1) (lambda...>
(define-values
  (add1)
  (lambda (x) (apply + '1 x)))

Chez Scheme 8.0:

> (expand '(define (add1 x) (+ 1 x)))
(begin
  (set! add1
    (lambda (x)
      (+ 1 x)))
  (void))

The lambda appears plain as day.

﹂绝世的画 2024-10-21 08:24:25

我依稀记得一位教授讨论过这样的事情。

我认为使用 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.

初见你 2024-10-21 08:24:25

当我使用 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!"

白首有我共你 2024-10-21 08:24:25

小计划者使用伪代码计划(出于教育目的进行简化并独立于实现)。今天的标准方案有一个定义的定义,您可以在其中隐式调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文