Using trace to display a procedure in racket

2019-04-30 04:04发布

I've been working through the last few exercises ch 1 of SICP, where several of the exercises uses higher-order functions. Currently I'm trying to debug a problem in my solution to 1.45, which is raising an arity mismatch. The function which is raising the error is the result of twice applying an averaging operation to a fixed-point function solver.

It would make my debugging efforts a lot easier if I could just dump some sort of representation of procedures, given that the procedure has been run through several other procedures that alter it before it raises an error. I've looked at the debugging documentation for DrRacket, added (require racket/trace) and (require errortrace) to my module and I think I'm familiar with the all the features of the debugging system -- but I still have no idea how to do this.

An answer for DrRacket would be ideal, but anything helps.

2条回答
够拽才男人
2楼-- · 2019-04-30 04:51

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.

查看更多
三岁会撩人
3楼-- · 2019-04-30 04:53

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.

查看更多
登录 后发表回答