[racket] syntax, differently

From: Marco Morazan (morazanm at gmail.com)
Date: Sun Aug 1 19:45:31 EDT 2010

> I'd also like to push for earlier introduction of (local ...) in
> HtDP/2e. We all know that code duplication is a bad thing and that
> giving complex constructs names is one of the easiest forms of
> abstraction, but we can't use local definitions to handle these cases
> until we've already done a lot of really complicated stuff. I'd like
> to be able to contrast:
>
> (define (distance p1 p2)
>  (sqrt (+ (sqr (- (posn-x p1) (posn-x p2)))
>           (sqr (- (posn-y p1) (posn-y p2))))))
>
> and
>
> (define (distance p1 p2)
>  (local
>    [(define x1 (posn-x p1))
>     (define y1 (posn-y p1))
>     (define x2 (posn-x p2))
>     (define y2 (posn-y p2))]
>    (sqrt (+ (sqr (- x1 x2)) (sqr (- y1 y2))))))
>
> fairly early, so that I can emphasize that, while the first has fewer
> keystrokes, the latter is easier for most people to read.
>

(define (distance p1 p2) (distance-helper (posn-x p1) (posn-y p1)
(posn-x p2) (posn-y p2)))

(define (distance-helper x1 y1 x2 y2) (sqrt (+ (sqr (- x1 x2)) (sqr (-
y1 y2)))))

What exactly does local contribute here? I am not convinced that more
syntax and the thorny-for-students semantics of local is really what
is needed.

-- 

Cheers,

Marco


Posted on the users mailing list.