6

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.

3
  • 3
    You should really add some code to illustrate the problem.
    – uselpa
    Jun 15, 2014 at 7:26
  • 1
    Possible duplicate of "How do you return the description of a procedure in Scheme?" ? Jun 15, 2014 at 13:25
  • What do you mean by "a representation of procedures"? This could mean an s-expression representing the source, the macro-expanded code, the compiled bytecode, or disassembled code from the JIT, or something else. Also, as an aside I think it's probably easier to write more unit tests, use contracts, or trace your functions rather than look at macro-expanded code or compiler output. Jun 16, 2014 at 16:24

2 Answers 2

7

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.

1
  • 1
    (trace +) produces an error in Racket 5.3.6 set!: cannot mutate module-required identifier in: +. On the other hand, (trace sum) does trace. Aug 25, 2014 at 2:54
2

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.