[racket] Enhancing a macro

From: Jeremiah Willcock (jewillco at osl.iu.edu)
Date: Wed Aug 22 20:57:53 EDT 2012

On Wed, 22 Aug 2012, Harry Spier wrote:

> I have a macro let**-debug (based on the let** macro from Matthias
> Felliesen) which gives me timing information.
> (define-syntax let**-debug
>  (syntax-rules ()
>    [(_ tmp body) (begin (displayln 'body) (time body))]
>    [(_ tmp a b ... body) (let ([tmp (begin (displayln 'a) (time a))])
> (let**-debug tmp b ... body))]))
> It works fine.  The idea is that I tune using the form let**-debug and
> when I'm happy with the results I change let**-debug to let** which
> still works the same without printing the timing information.

What about using a parameter:

(define timing-indent-level (make-parameter 0))

(define-syntax let**-debug
   (syntax-rules ()
     [(_ tmp body)
      (begin (displayln 'body)
             (time (begin0 (display (make-string (timing-indent-level) #\space)) body)))]
     [(_ tmp a b ... body)
      (let ([tmp (begin (displayln 'a)
                        (time (begin0
                                (parameterize ([timing-indent-level (+ 2 (timing-indent-level))]) a)
                                (display (make-string (timing-indent-level) #\space)))))])
        (let**-debug tmp b ... body))]))

This assumes that the operation that you're timing is cheap compared to 
printing things out and modifying parameters.  If it's not, you may need 
to use time-apply and print out the results yourself.

-- Jeremiah Willcock

Posted on the users mailing list.