[racket] internal define in define

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Sep 22 13:20:48 EDT 2011

This subject came up a bit more than a year ago.  One thing that I
mentioned at the time is that you can also imagine allowing definition
before any expression, and they'd get lumped with it, for example:

| (define counter-or-not
|   (if zeros?
|     (lambda () 0)
|     (define n 0)
|     (lambda () (set! n (add1 n)) n)))

or:

| (if (> (define (square n) (* n n))
|        (- (square x) (square y))
|        0)
|   ...blah...)

but that has the obvious problem of being ambiguous (does the last `n'
definition have the `0' part in its scope?).  The alternative is
something that Matthew mentioned later on a post to the dev list:

| More Internal-Definition Contexts
| ---------------------------------
| 
| Internal definitions could be allowed in more places, such as
| 
|  (define f
|    (define x 2)
|    x)
| 
| In principle, where a single expression is expected, definitions could
| be allowed to precede the expression.
| 
| It's a tempting generalization, but probably too confusing. I think
| it's better to use some form that groups definitions with an
| expression: `(let () ....)', `(block ....)', or something like that.

But IMO the difference between (let () ...) and (block ...) is
negligible.  (The `block' form has the advantage of not relying on an
obscure idiom that is abusing `let', but still as far as writing code
goes, it's a minor difference.)

Yet another option that I mentioned was:

|   (+ 1 { n = 8
|          (sqr x) = (* x x)
|          (sqr n) })

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!


Posted on the users mailing list.