The naive model for begin is to think of it as a function that returns its last 
argument (assuming left-to-right evaluation). 
So that doesn’t scale.

The example without begin you mention indeed behaves the same, and that follows 
from the model of “let has an implicit begin” that I tell them about.

So at least to me, the complexity comes from understanding begin properly.

Thanks,

-- Éric


> On Nov 3, 2015, at 11:59 AM, Scott Moore <sdmo...@fas.harvard.edu> wrote:
> 
> 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 
> <mailto: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 
>>> <mailto: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
>>>  
>>> <https://urldefense.proofpoint.com/v2/url?u=http-3A__docs.racket-2Dlang.org_reference_syntax-2Dmodel.html-23-2528part.-5Fintdef-2Dbody-2529&d=CwMFaQ&c=WO-RGvefibhHBZq3fL85hQ&r=OPR-Xys5wfSBIeTkWaH0D_htBR-X7qY24pTHU6ib2iM&m=SB0ys8d9nuzR8hLakyqx5MZ5TQiQW_UpwySsBUxWg30&s=5ElZW1aRhlESddZVvuawVv30hjKxA5PbsHq3AAQgUUU&e=>
>>> 
>>> 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 
>>> <mailto: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 <mailto: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 
>>>>> <mailto: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 
>>>>> > <mailto: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 
>>>>> > <mailto:racket-users+unsubscr...@googlegroups.com>.
>>>>> > For more options, visit https://groups.google.com/d/optout 
>>>>> > <https://urldefense.proofpoint.com/v2/url?u=https-3A__groups.google.com_d_optout&d=CwMFaQ&c=WO-RGvefibhHBZq3fL85hQ&r=OPR-Xys5wfSBIeTkWaH0D_htBR-X7qY24pTHU6ib2iM&m=SB0ys8d9nuzR8hLakyqx5MZ5TQiQW_UpwySsBUxWg30&s=cyfarVv9aewI9mgi-OMl5YdbYwxsRtTH7z9ktvDxEbI&e=>.
>>>>> >
>>>>> >
>>>>> > --
>>>>> > 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 
>>>>> > <mailto:racket-users+unsubscr...@googlegroups.com>.
>>>>> > For more options, visit https://groups.google.com/d/optout 
>>>>> > <https://urldefense.proofpoint.com/v2/url?u=https-3A__groups.google.com_d_optout&d=CwMFaQ&c=WO-RGvefibhHBZq3fL85hQ&r=OPR-Xys5wfSBIeTkWaH0D_htBR-X7qY24pTHU6ib2iM&m=SB0ys8d9nuzR8hLakyqx5MZ5TQiQW_UpwySsBUxWg30&s=cyfarVv9aewI9mgi-OMl5YdbYwxsRtTH7z9ktvDxEbI&e=>.
>>>>> 
>>>>> --
>>>>> 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 
>>>>> <mailto:racket-users+unsubscr...@googlegroups.com>.
>>>>> For more options, visit https://groups.google.com/d/optout 
>>>>> <https://urldefense.proofpoint.com/v2/url?u=https-3A__groups.google.com_d_optout&d=CwMFaQ&c=WO-RGvefibhHBZq3fL85hQ&r=OPR-Xys5wfSBIeTkWaH0D_htBR-X7qY24pTHU6ib2iM&m=SB0ys8d9nuzR8hLakyqx5MZ5TQiQW_UpwySsBUxWg30&s=cyfarVv9aewI9mgi-OMl5YdbYwxsRtTH7z9ktvDxEbI&e=>.
>> 
> 
> -- 
> 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 
> <mailto:racket-users+unsubscr...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout 
> <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