关于《Land of Lisp》中的 lisp Lambda 函数示例的问题
我不太了解 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
edge
参数来自(cdr 节点)
中的项目。您的内部 lambda 将为(cdr 节点)
中的每个元素调用一次。试试这个例子:
或者,使用字面 lambda:
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:
Or, with a literal lambda: