gpt4 book ai didi

debugging - 将构造函数和选择器定义为 cons、car 和 cdr 是否仍然不可取?

转载 作者:行者123 更新时间:2023-12-03 20:23:49 24 4
gpt4 key购买 nike

计算机程序的结构和解释有以下footnote :

Another way to define the selectors and constructor is

(define make-rat cons)
(define numer car)
(define denom cdr)

The first definition associates the name make-rat with the value of the expression cons, which is the primitive procedure that constructs pairs. Thus make-rat and cons are names for the same primitive constructor.

Defining selectors and constructors in this way is efficient: Instead of make-rat calling cons, make-rat is cons, so there is only one procedure called, not two, when make-rat is called. On the other hand, doing this defeats debugging aids that trace procedure calls or put breakpoints on procedure calls: You may want to watch make-rat being called, but you certainly don't want to watch every call to cons.


这个建议是否仍然适用?例如,现代调试辅助工具是否仍然以这种方式被击败?

最佳答案

在 Common Lisp 中也可以做到这一点。我们可以设置符号的符号函数。

(setf (symbol-function 'numer)
(function car))
另一种方法是定义这些函数:
(defun numer (rat)
(car rat))
现在将有调用这些额外函数的开销。这可以在开发和调试期间提供帮助。
在 Common Lisp 中,可以给编译器一个提示,它可以内联函数:
(declaim (inline numer))
然后在用于生产或交付的优化编译代码中,可以内联函数:函数调用开销将不存在,但调用将不再可见。

关于debugging - 将构造函数和选择器定义为 cons、car 和 cdr 是否仍然不可取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65832849/

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