I think its more a complexity of “define” than of “begin” (though begin is 
certainly tricky be because it can introduce and internal definition context).

Your student’s program has the same behavior as this program without the begin, 
since the let also introduces a definition context:

(let ([y 5])
  (display y)
  (define y 10)
  y)
On November 3, 2015 at 8:51:38 AM, Éric Tanter (etan...@dcc.uchile.cl) wrote:

Thanks all! This is helpful — the mental model of begin that I presented to my 
students was too simple to account for defines.

-- Éric


On Nov 2, 2015, at 10:00 PM, Scott Moore <sdmo...@fas.harvard.edu> wrote:

The relevant part of the reference for these “internal definition contexts” is 
here: 
http://docs.racket-lang.org/reference/syntax-model.html#%28part._intdef-body%29

That’s a very operational description, but amounts to saying “the bindings from 
a define in an internal definition context are visible to all other 
definitions/expressions in the same internal definition context."
On November 2, 2015 at 7:52:43 PM, Scott Moore (sdmo...@fas.harvard.edu) wrote:

Does it help to think of let and begin as introducing a new scope, and define 
as having “block scope” (or whatever they call this sort of thing in trendy 
languages like javascript)?

I’m guessing the reason you found this to be unintuitive is that the ‘y’ in 
(display y) refers to something that comes after it in the source, which would 
not be the case if the semantics of define were more like a let:

(let ([y 5])
  (begin
     (display y)
     (define y 10)
     y))))

=>

(let ([y 5])
  (begin
     (display y)
     (let ([y 10])
        y))))

But that ‘y’ appears in the scope of the begin, which is the scope in which the 
define inserts it’s bindings.

Not sure if this was helpful…
On November 2, 2015 at 7:39:16 PM, Robby Findler (ro...@eecs.northwestern.edu) 
wrote:

Yeah, perhaps I've drunk too much of the koolaid, but I'm not even
seeing an alternative interpretation that makes any sense!

Does it help to see the arrows in DrRacket? In particular the upward
pointing one that points at the 'y' in display's argument?

Robby


On Mon, Nov 2, 2015 at 6:32 PM, Alex Knauth <alexan...@knauth.org> wrote:
> This is because begin can have potentially recursive and mutually recursive
> definitions in it.
>
> This does the same thing:
> (let ([y 5])
> (local [(define x y) ; this y should be bound to
> (define y 10)] ; <- this y, but it is used too early
> y))
>
> While this slightly different case works fine:
> (let ([y 5])
> (local [(define (x) y) ; this y is bound to
> (define y 10)] ; <- this y, but it is within a function
> y))
>
> When the first y in the local is within a function, it isn't evaluated until
> the function is called, so that's fine.
>
> But when the first y in the local is evaluated right away, before it is
> defined, it raises an error.
>
> It doesn't make sense for it to be bound to the outer y, because then it
> would be inconsistent with the version where it is within a function.
>
>
> On Nov 2, 2015, at 7:13 PM, Éric Tanter <etan...@dcc.uchile.cl> wrote:
>
> Hi all,
>
> Some of my creative students came up with the following:
>
> (let ([y 5])
> (begin
> (display y)
> (define y 10)
> y)))
>
> which raises a mysterious
> y: undefined;
> cannot use before initialization
>
> I remember earlier discussion on this list about the fact that `define' was
> somehow “broken” for various cases of nesting, and that it was part of the
> motivation for coming up with `local’. Sure enough, the following works as
> expected:
>
> (let ([y 5])
> (begin
> (display y)
> (local [(define y 10)]
> y)))
>
> While I don’t intend to motivate my students to do funky let/define nesting,
> I would like to be able to explain why the error is raised. Any (sensible)
> explanation?
>
> Thanks!
>
> -- Éric
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to