gpt4 book ai didi

macros - 是否可以将以下 lisp 宏编写为函数?

转载 作者:行者123 更新时间:2023-12-02 06:53:46 25 4
gpt4 key购买 nike

我正在阅读Paul Graham's ANSI Common Lisp 。在有关宏的章节中,他展示了以下示例:

(defmacro in (obj &rest choices)
(let ((insym (gensym)))
`(let ((,insym ,obj))
(or ,@(mapcar #'(lambda (c) `(eql ,insym ,c))
choices)))))

(如果第一个参数等于任何其他参数,则返回 true)

他认为不能将其写成函数。这个函数不具有相同的功能吗?

(defun in (obj &rest choices)
(reduce (lambda (x y)
(or x (eql y obj)))
choices
:initial-value nil))

我看到的区别是宏只会评估参数,直到找到 eql 参数。是这样吗?

最佳答案

重点是,宏版本会延迟计算参数(它扩展为 OR),如果找到匹配项,则会停止。这无法通过函数实现,因为 funcall 总是首先计算所有参数。

关于macros - 是否可以将以下 lisp 宏编写为函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10570847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com