Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using trace to display a procedure in racket

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.

like image 824
LiavK Avatar asked Jun 15 '14 05:06

LiavK


1 Answers

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.

like image 94
David Merinos Avatar answered Sep 25 '22 00:09

David Merinos