This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#3826 2011-10-13 23:59:35

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

Today's vocabulary words

What happens if a custom block or lambda uses a variable name that isn't its own input or script variable?

Most languages designed for kids (e.g., Logo and Smalltalk) use dynamic scope, which means that the interpreter looks at the procedure that called this one, then at the procedure that called that one, and so on until you reach the global variables.  This is an easy rule to understand, because if a variable exists, you can use it.

Most modern languages for adults (e.g., Scheme, Javascript) use lexical scope, which means that the interpreter looks at the procedure inside which this one was created, then at the one outside of that, and so on until you reach the global variables.  This rule leads to faster compiled programs, and allows the style of OOP explained in section VI of the BYOB 3.1 manual.

Today's Snap! feature

Snap! uses hybrid scope:  First it looks in the lexical scope of the procedure, all the way out to the global variables.  But if there is no variable by the desired name in the lexical environment, instead of giving an error message, Snap! looks in the procedure's dynamic environment.  (More precisely, it looks at the caller's entire lexical environment, then at the caller's caller's entire lexical environment, etc.)  This means that if the user is careful not to use the same name for dynamic and lexical variables, they can be combined -- lexical to allow explicit OOP, and dynamic so that an input to the main procedure of a project can be used everywhere without having to be passed down as input to each helper procedure.

http://byob.berkeley.edu/abagghea.png


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#3827 2011-10-14 05:41:48

xly
Scratcher
Registered: 2010-04-17
Posts: 100+

Re: BYOB 3 - Discussion Thread

@bharvey
Not obvious ! But it works. No need to say that a and g have to be defined as "global"variables".

Offline

 

#3828 2011-10-14 06:32:37

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: BYOB 3 - Discussion Thread

Oh sure, you're right, Xavier, a and g are globals. Thanks for pointing that out!


Jens Mönig

Offline

 

#3829 2011-10-14 10:01:48

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: BYOB 3 - Discussion Thread

G is a script variable.  hmm

Offline

 

#3830 2011-10-14 10:08:01

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

scimonster wrote:

G is a script variable.

There is a script variable named G, and it's in the dynamic scope of the arithmetic computation in C, but there's also a global G (it's set down at the bottom), and global variables are part of the lexical scope of C, so they take priority.

Dynamic scope:  C is called by B which is called by A which is called in the global environment.

Lexical scope:  C is defined inside A which is defined in the global environment.

In this example only the name DYN is resolved using dynamic scope.


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#3831 2011-10-14 10:30:12

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: BYOB 3 - Discussion Thread

bharvey wrote:

scimonster wrote:

G is a script variable.  hmm

There is a script variable named G, and it's in the dynamic scope of the arithmetic computation in C, but there's also a global G (it's set down at the bottom), and global variables are part of the lexical scope of C, so they take priority.

Dynamic scope:  C is called by B which is called by A which is called in the global environment.

Lexical scope:  C is defined inside A which is defined in the global environment.

In this example only the name DYN is resolved using dynamic scope.

I don't understand most of this anyways.  tongue

Offline

 

#3832 2011-10-14 11:39:33

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

scimonster wrote:

I don't understand most of this anyways.

When I was a high school teacher I used to say "anyhows" to my students in an effort to get them to stop saying "anyways" but it never worked. :-/


You can understand this; it's not as weird an idea as continuations.  I'm just not explaining well enough.


Look at the code in the picture.  Down at the bottom it says CALL A..., so that means that from top level (where there are only global variables, so we call it the "global environment") we call A.  A calls B, and B calls C.  So C>B>A>Global is the dynamic environment.

But now, instead of looking at who calls whom, look at where things are defined -- where the lambdas (the THE BLOCKs) are.  A's lambda isn't inside any other lambda (or custom block, but we don't have those yet, and anyway those are just another kind of lambda with a slicker GUI).  B's lambda is inside A, and C's lambda is also inside A (but not inside B).  So C's lexical environment is C>A>Global.

Got that?  If not, ask a question.

So, inside of C we're trying to use four variables.  For each variable reference, we look first in the lexical environment, then in the dynamic environment.  In each environment, we take the first one we find, starting from C's lambda itself.

Lexical environment
C: IN
A: OUT, B, C
Global: A, G

Dynamic environment
C: IN
B: DYN, G, IN
A: OUT, B, C
Global: A, G

In the above, "A: OUT, B, C" means that inside A's lambda there are bindings for variables OUT (an input to the lambda), B, and C (script variables), etc.

So, where do we find G?  First we look in the lexical environment.  We don't find it in C, nor in A, but we do find it in the global environment, so that's the one we use.

Where do we find OUT?  Not in C, but in A.

Where do we find IN?  It's right there in C.

Finally, where do we find DYN?  It's not in the lexical environment at all, so for this variable only we look in the dynamic environment and find it in B.

(In most programs, the lexical environment is a subset of the dynamic environment.  The only exception is when a procedure reports an internally generated procedure:

THE [REPORT [THE ... BLOCK]] BLOCK. INPUT NAMES: FOO

When the reported procedure (the one made by the inner THE BLOCK) is called, its lexical environment will include the variable FOO, but its dynamic environment won't.)

Okay?


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#3833 2011-10-14 16:05:48

xly
Scratcher
Registered: 2010-04-17
Posts: 100+

Re: BYOB 3 - Discussion Thread

In the meantime ...Cos x graph

http://www.xleroy.net/Byobtuto/New/Snap!/cosxgraph.gif

Offline

 

#3834 2011-10-14 16:07:01

14God
Scratcher
Registered: 2008-11-14
Posts: 100+

Re: BYOB 3 - Discussion Thread

I see it's time to update my sign off banner  smile


http://cs.berkeley.edu/~bh/sig4.png
Logic and reason have led me to atheism... but I'm stuck with the name  tongue

Offline

 

#3835 2011-10-15 00:02:22

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

14God wrote:

I see it's time to update my sign off banner  smile

I was wondering if anyone was ever going to notice the logo!  Great design by nXIII.


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#3836 2011-10-15 12:21:45

shadow_7283
Scratcher
Registered: 2007-11-07
Posts: 1000+

Re: BYOB 3 - Discussion Thread

nXIII? Where has he been?

Offline

 

#3837 2011-10-15 12:44:31

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

shadow_7283 wrote:

nXIII? Where has he been?

Taking a class that includes an assignment to design a logo for a client.  big_smile


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#3838 2011-10-15 13:05:09

shadow_7283
Scratcher
Registered: 2007-11-07
Posts: 1000+

Re: BYOB 3 - Discussion Thread

That's amazing...  tongue

Offline

 

#3839 2011-10-15 21:06:27

14God
Scratcher
Registered: 2008-11-14
Posts: 100+

Re: BYOB 3 - Discussion Thread

He did a good job  smile


http://cs.berkeley.edu/~bh/sig4.png
Logic and reason have led me to atheism... but I'm stuck with the name  tongue

Offline

 

#3840 2011-10-16 11:47:08

Sidharth
Scratcher
Registered: 2007-12-14
Posts: 100+

Re: BYOB 3 - Discussion Thread

Please tell me I'm doing something terribly wrong with my recursion here:

http://i.imgur.com/7CT1h.png

when I http://i.imgur.com/q4S2F.png, it didn't work.

when I use 0 instead of 1, it works, though


http://www.danasoft.com/citysign.jpg

Offline

 

#3841 2011-10-16 16:01:14

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

Sidharth wrote:

Please tell me I'm doing something terribly wrong with my recursion here:

For me it doesn't work even with 0.  I hope that means Jens is working on it.  smile


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#3842 2011-10-16 17:54:54

xly
Scratcher
Registered: 2010-04-17
Posts: 100+

Re: BYOB 3 - Discussion Thread

@ Sidharth & bharvey
For me it doesn't work whichever is the value.

Offline

 

#3843 2011-10-16 19:36:04

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: BYOB 3 - Discussion Thread

the body's first two lines have to say:

   if n = 1
      report 1

otherwise it keeps stuck in an infinite loop hitting zero and reporting 1.


Jens Mönig

Offline

 

#3844 2011-10-16 21:04:02

Sidharth
Scratcher
Registered: 2007-12-14
Posts: 100+

Re: BYOB 3 - Discussion Thread

Jens wrote:

the body's first two lines have to say:

   if n = 1
      report 1

otherwise it keeps stuck in an infinite loop hitting zero and reporting 1.

Ahh ok, it works now. Thanks!  smile


http://www.danasoft.com/citysign.jpg

Offline

 

#3845 2011-10-17 09:30:17

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

Jens wrote:

the body's first two lines have to say:

   if n = 1
      report 1

otherwise it keeps stuck in an infinite loop hitting zero and reporting 1.

Wait, why?  0!=1.  That's the right base case.  If it reports 1, why is there an infinite loop?  If it reported factorial(1) or something, that would be an infinite loop.

What am I missing?

EDIT:  Turns out to be a Javascript misfeature; Jens is working on it.

Last edited by bharvey (2011-10-17 16:53:16)


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#3846 2011-10-17 11:57:00

xly
Scratcher
Registered: 2010-04-17
Posts: 100+

Re: BYOB 3 - Discussion Thread

@bharvey
Anyhow the Jens way works.
As follows the same problem - cos x graph - but now using CALL, REPORT, and Recursive way for ITEM. See :

http://www.xleroy.net/ByobTuto/New/Snap!/cosxgraphrec.gif

Offline

 

#3847 2011-10-17 17:27:25

14God
Scratcher
Registered: 2008-11-14
Posts: 100+

Re: BYOB 3 - Discussion Thread

I wish you could save projects so I could use xly's projects ;(


http://cs.berkeley.edu/~bh/sig4.png
Logic and reason have led me to atheism... but I'm stuck with the name  tongue

Offline

 

#3848 2011-10-17 18:57:10

xly
Scratcher
Registered: 2010-04-17
Posts: 100+

Re: BYOB 3 - Discussion Thread

@14God
Right-click on the image to either Display it or Save it. Then it is easy to copy the code.

Offline

 

#3849 2011-10-18 02:37:42

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: BYOB 3 - Discussion Thread

BYOB curlicules:
http://www.imgpaste.com/KsuW.png


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#3850 2011-10-18 02:46:35

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: BYOB 3 - Discussion Thread

This works in BYOB but not in Snap!
http://www.imgpaste.com/3Vrm.png
EDIT: It's the ()^() block as a variable.
EDIT: Can you add ()^() as a Snap! feature?

Last edited by Hardmath123 (2011-10-18 02:48:48)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

Board footer