在(trace procedure)的Guile方案和Chez方案的(trace-let (bindings) body)跟踪工具中有哪些等价物。
我已经查看了https://www.gnu.org/software/guile/manual/html_node/Tracing-Traps.html上的文档,但我不知道如何使用源代码文件中的Guile跟踪过程,而不是Guile REPL中的过程,该过程可以在给定先前导入的模块(use-modules (system vm trace))的情况下使用,trace (procedure application)完成。
我对从源代码跟踪类似于(trace fact1)的递归过程的应用程序很感兴趣,并将下面的输出输出到控制台
trace: (fact1 4)
trace: | (fact1 3)
trace: | | (fact1 2)
trace: | | | (fact1 1)
trace: | | | | (fact1 0)
trace: | | | | 1
trace: | | | 1
trace: | | 2
trace: | 6
trace: 24可以在Guile中跟踪命名的let (let name (bindings) body)语法扩展吗?在研究过程的尾递归实现时,需要这样做。
非常感谢!
发布于 2020-07-17 07:25:42
参见add-trap-at-procedure-call!在https://www.gnu.org/software/guile/docs/docs-2.0/guile-ref/High_002dLevel-Traps.html页面上。
举个例子:
(define (factorial n)
(fact-iter 1 1 n))
(define (fact-iter product counter max-count)
(if (> counter max-count)
product
(fact-iter (* counter product)
(+ 1 counter)
max-count)))
(add-trace-at-procedure-call! fact-iter)
(add-trace-at-procedure-call! factorial)
(factorial 6)
$41 = 7
$42 = 8
Trap 8: (factorial 6)
Trap 7: (fact-iter 1 1 6)
Trap 7: | (fact-iter 1 2 6)
Trap 7: | | (fact-iter 2 3 6)
Trap 7: | | | (fact-iter 6 4 6)
Trap 7: | | | | (fact-iter 24 5 6)
Trap 7: | | | | | (fact-iter 120 6 6)
Trap 7: | | | | | | (fact-iter 720 7 6)
Trap 7: | | | | | | 720
Trap 7: | | | | | 720
Trap 7: | | | | 720
Trap 7: | | | 720
Trap 7: | | 720
Trap 7: | 720
Trap 7: 720
Trap 8: 720
$43 = 720factorial & fact-iter取自SICP 1.2.1。
我不知道如何让它显示尾递归,或者这是否意味着没有尾递归调用优化。但它能追踪到。
https://stackoverflow.com/questions/61983739
复制相似问题