Using trace to display a procedure in racket

人盡茶涼 提交于 2019-12-05 10:54:35

Adding (require racket/trace) won't throw any procedure displays in the console. You want to use (trace function-name) this will print purple (default color) lines in the console when you use the given function in the trace call. Example

(define sum (λ (x y) (+ x y)))
(define multiply
  (λ (x y)
    (multiply-aux x y x)
    ))
(define multiply-aux (λ (x y res) 
                       (if (= y 0) 0 
                           (if (= y 1) res 
                               (multiply-aux x (- y 1) (sum res x))))))
(require racket/trace)
(trace sum)

In the console:

> (multiply 4 5)
>(sum 4 4)
<8
>(sum 8 4)
<12
>(sum 12 4)

Tested in DrRacket 6.0.1

Let me know if you need more help.

One trick is to create an alias for a primitive function. So if you want to trace the addition operator somewhere, trace won't allow it unless you do this:

(require trace) (define *+ +)

Then use *+ anywhere in the code where you want to watch its output closely, without seeing the output of + used elsewhere.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!