[racket] A define* form [was: Why internal definitions?]

From: Neil Toronto (neil.toronto at gmail.com)
Date: Wed Nov 16 12:49:17 EST 2011

On 11/15/2011 10:03 PM, Jay McCarthy wrote:
> It would be nice if we had a define* like define-package has.

Yes! There are a lot of cases where I protect myself from future 
stupidity by shadowing identifiers for which referring to them again is 
an error.

This is from collects/plot/plot2d/clip.rkt:

     (let*-values ([(xs ys)  (clip-polygon-x-min x-min xs ys)]
                   [(xs ys)  (clip-polygon-x-max x-max xs ys)]
                   [(xs ys)  (clip-polygon-y-min y-min xs ys)]
                   [(xs ys)  (clip-polygon-y-max y-max xs ys)])
	....)

To use defines instead, I'd have to number all the identifiers:

     (define-values (xs1 ys1) (clip-polygon-x-min x-min xs ys))
     (define-values (xs2 ys2) (clip-polygon-x-max x-max xs1 ys1))
     (define-values (xs3 ys3) (clip-polygon-y-min y-min xs2 ys2))
     (define-values (xs4 ys4) (clip-polygon-y-max y-max xs3 ys2))
     ....

and then never refer to xs, xs1, xs2, xs3, ys, ys1, ys2, or ys3 again.

(Did you spot the error in the above? It's *really easy* to make, and is 
also easy to miss.)

At the Scheme workshop this year, I gave Eli some code for crossing out 
picts in slideshows, that used shadowing to ensure correctness. (He 
needed it for his rushed but excellent talk.) He promptly flattened it 
to defines, and broke it. So I'm not the only one.

Neil T


Posted on the users mailing list.