[racket] Why internal definitions?

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed Nov 16 07:53:40 EST 2011

On Nov 15, 2011, at 11:50 PM, Jordan Schatz wrote:

> From Racket v5.2 release notes:
> 
>> Internal-definition expansion has changed to use let* semantics for
>> sequences that contain no back references. This change removes a
>> performance penalty for using internal definitions instead of let in
>> common cases, and it only changes the meaning of programs that capture
>> continuations in internal definitions. Internal definitions are now
>> considered preferable in style to let.
> 
> I'm not sure that I understand, but if I have it figured out then this:
> 
> (define (foo x)
>  (local [(define i 10)
>          (define j 12)]
>         (+ x i j)))
> 
> Is now considered better style then this?
> 
> (define (foo2 x)
>  (let ([i 10]
>        [j 12])
>    (+ x i j)))
> 
> Why?


Your question suggests that you come from a teaching language 
background where we introduce only local definitions. In ISL
and ISL+lambda, the use of local makes it easier to move global
transformations into a local scope and vice versa. Most importantly, 
these movement preserve the exact semantics of the definitions, 
including errors. 

An *internal* definition differs slightly from a local definition
in syntax and *semantics*: 

(define (foo x)
  (define i 10)
  (define j 12)
  (+ i j x))

It's preferable to local defines for aesthetic reasons because it 
consumes less horizontal space (a smaller indentation). 

-- Matthias



Posted on the users mailing list.