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

标签 debugging scheme lisp sicp cons

计算机程序的结构和解释有以下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/

相关文章:

lisp - SICP 练习 1.37 : My iterative solution got the right answer but got wrong in 1. 38

html - 在 Google Chrome 中显示 HTML 错误?

c++ - 使用宏来显示宏的字符串化内容

Android - 调试引用项目问题

debugging - 有没有逐步进入CasperJS代码和调试的方法

lisp - Scheme 中是否有等同于 Lisp 的 "runtime"原语?

algorithm - 混合函数的增长顺序

macros - 如何在 Racket 中使用宏重命名过程?

types - 在 Common Lisp 中将字符转换为整数

lisp - 在 Lisp 中计算平均绩点和类(class)平均水平