[racket] Enhancing a macro

From: Harry Spier (vasishtha.spier at gmail.com)
Date: Wed Aug 22 19:56:08 EDT 2012

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.

For example:

(define (image-file->vectorof-rowlistof-black-strip-ranges image-file-path)
  (let**-debug tmp (MagickReadImage image-file-path)
          (begin (set-image-background-white tmp) tmp)
          (begin (MagickThresholdImage tmp 30000) tmp)
          (begin (MagickDeskewImage tmp 26000) tmp)
          (begin (MagickThresholdImage tmp 62000) tmp)
          (begin (MagickWriteImage tmp (put-devanagari-page-dialog))tmp)
          (MagickExportImagePixels  tmp 0

(MagickGetImageWidth  tmp)

(MagickGetImageHeight tmp) "I")
          (for/vector ( [rowof-intensities (in-vector tmp)])

(bytestring-intensities->listof-ranges-of-black-pixels
rowof-intensities) )))

gives the output:
--------------------
(MagickReadImage image-file-path)
cpu time: 62 real time: 54 gc time: 0
. . .
. . .
(for/vector ((rowof-intensities (in-vector tmp)))
(bytestring-intensities->listof-ranges-of-black-pixels
rowof-intensities))
cpu time: 31 real time: 27 gc time: 0
-----------------
But the above function containing the let**-debug macro is called from
another let-debug** form
I.e.
(let**-debug page (path-to-image-of-page)
               (image-file->vectorof-rowlistof-black-strip-ranges page)
               (vectorof-rowlistof-black-strip-ranges->vectorof-strip-nodes
page)
               (vectorof-strip-nodes->vectorof-blobs page)))

So the output is:
---------------------------------
(path-to-image-of-page)
cpu time: 890 real time: 5964 gc time: 0
(image-file->vectorof-rowlistof-black-strip-ranges page)
(MagickReadImage image-file-path)
cpu time: 62 real time: 54 gc time: 0
. . .
. . .
(for/vector ((rowof-intensities (in-vector tmp)))
(bytestring-intensities->listof-ranges-of-black-pixels
rowof-intensities))
cpu time: 31 real time: 27 gc time: 0
cpu time: 3744 real time: 8018 gc time: 0
(vectorof-rowlistof-black-strip-ranges->vectorof-strip-nodes page)
cpu time: 687 real time: 745 gc time: 187
(vectorof-strip-nodes->vectorof-blobs page)
cpu time: 140 real time: 130 gc time: 0
--------------


But what would be more convenient  is if the output was indented for
each nested let**-debug so the output would look like this.
--------------
(path-to-image-of-page)
cpu time: 890 real time: 5964 gc time: 0
(image-file->vectorof-rowlistof-black-strip-ranges page)
	(MagickReadImage image-file-path)
	cpu time: 62 real time: 54 gc time: 0
	. . .
	. . .
	(for/vector ((rowof-intensities (in-vector tmp)))
(bytestring-intensities->listof-ranges-of-black-pixels
rowof-intensities))
	cpu time: 31 real time: 27 gc time: 0
cpu time: 3744 real time: 8018 gc time: 0
(vectorof-rowlistof-black-strip-ranges->vectorof-strip-nodes page)
cpu time: 687 real time: 745 gc time: 187
(vectorof-strip-nodes->vectorof-blobs page)
cpu time: 140 real time: 130 gc time: 0
----------------------------

Can someone show me how to enhance the let**-debug macro so that if a
let**-debug form is in a procedure called from within another
let**-debug form its output will be indented from the output of the
calling let**-debug form.

Thanks,
Harry Spier

Posted on the users mailing list.