关于《Land of Lisp》中的 lisp Lambda 函数示例的问题

发布于 2024-12-04 20:29:05 字数 1093 浏览 1 评论 0原文

我不太了解 lambda 函数。这是《Land of Lisp》一书中的示例函数:

(defun edges->dot (edges)
  (mapc (lambda (node)
          (mapc (lambda (edge)
                  (fresh-line)
                  (princ (dot-name (car node)))
                  (princ "->")
                  (princ (dot-name (car edge)))
                  (princ "[label=\"")
                  (princ (dot-label (cdr edge)))
                  (princ "\"];"))
                (cdr node)))
        edges))

现在让我们看看内部部分:

(mapc (lambda (edge)
        (fresh-line)
        (princ (dot-name (car node)))
        (princ "->")
        (princ (dot-name (car edge)))
        (princ "[label=\"")
        (princ (dot-label (cdr edge)))
        (princ "\"];"))
      (cdr node)))

我知道函数 mapc 接受两个参数,一个函数和一个列表。我还了解,通过使用 lambda (node) ,我传递了一个匿名函数,该函数将一个参数 (node) 作为第一个参数传递给 mapc ,并且(cdr节点)将用作mapc的第二个参数。至少我认为这就是正在发生的事情!

我不明白的是我的匿名函数在哪里获取 (lambda (edge) 中的 edge 的值。如果有人可以向我解释这一点,我将不胜感激。

I'm not quite understanding lambda functions. Here is an example function from the book Land of Lisp:

(defun edges->dot (edges)
  (mapc (lambda (node)
          (mapc (lambda (edge)
                  (fresh-line)
                  (princ (dot-name (car node)))
                  (princ "->")
                  (princ (dot-name (car edge)))
                  (princ "[label=\"")
                  (princ (dot-label (cdr edge)))
                  (princ "\"];"))
                (cdr node)))
        edges))

Let's just look at the inner part here for now:

(mapc (lambda (edge)
        (fresh-line)
        (princ (dot-name (car node)))
        (princ "->")
        (princ (dot-name (car edge)))
        (princ "[label=\"")
        (princ (dot-label (cdr edge)))
        (princ "\"];"))
      (cdr node)))

I understand that the function mapc takes two arguments, a function and a list. I also understand that by using lambda (node) I am passing an anonymous function that takes one argument (node) as the first argument to mapc, and that (cdr node) will be used as the second argument to mapc. At least I think that's what's going on!

What I don't understand is where my anonymous function gets the value for edge in (lambda (edge). I would appreciate it if someone could please explain this to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

情丝乱 2024-12-11 20:29:05

edge 参数来自 (cdr 节点) 中的项目。您的内部 lambda 将为 (cdr 节点) 中的每个元素调用一次。

试试这个例子:

(mapc #'princ '(1 2 3 4 5))

或者,使用字面 lambda:

(mapc #'(lambda (x)
          (princ x)
          (terpri))
      '(1 2 3 4 5))

The edge argument comes from the items in (cdr node). Your inner lambda will be called once for each element in (cdr node).

Try this for example:

(mapc #'princ '(1 2 3 4 5))

Or, with a literal lambda:

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