1 00:00:06,300 --> 00:00:09,436 You've already seen that local lets you have definitions that are available 2 00:00:09,436 --> 00:00:14,200 inside the local. But what does that mean exactly? 3 00:00:14,200 --> 00:00:17,752 Are they available outside? What if there's a definition outside the 4 00:00:17,752 --> 00:00:21,110 local and the inside the local that has the same name? 5 00:00:21,110 --> 00:00:24,970 What if there's a Local inside a Local, or even one more Local inside of that? 6 00:00:26,340 --> 00:00:29,994 In this video, I'm going to talk about the concept of lexical scoping, and show 7 00:00:29,994 --> 00:00:34,940 how scope contours will let you answer any such question about Local. 8 00:00:36,830 --> 00:00:41,108 In this video, I'm going to talk more precisely about what it means to say that 9 00:00:41,108 --> 00:00:46,220 Local sets up some definitions inside the local expression. 10 00:00:46,220 --> 00:00:50,188 To do that I'm going to introduce an important concept in programming called 11 00:00:50,188 --> 00:00:53,704 lexical scoping. Let me set up a little problem here for 12 00:00:53,704 --> 00:00:59,522 us to think about. Let me get rid of that local expression 13 00:00:59,522 --> 00:01:07,833 and I'll say define t is incendio. Now the question is: If I run this 14 00:01:07,833 --> 00:01:12,974 program what am I going to get? Am I going to get accio port key or 15 00:01:12,974 --> 00:01:18,020 incendio port key? I'll put a space there. 16 00:01:18,020 --> 00:01:22,750 Now that's not key to the problem. Which of the two am I going to get? 17 00:01:22,750 --> 00:01:26,290 Well, I'm going to get Accio port key, which is the one I want of course, but 18 00:01:26,290 --> 00:01:29,939 the question is why? And in order to talk about that, I'm 19 00:01:29,939 --> 00:01:32,855 going to introduce this concept of lexical scoping. 20 00:01:32,855 --> 00:01:37,535 And scope contours. So first thing to understand is that in a 21 00:01:37,535 --> 00:01:41,825 program there's a thing called the top level scope, the global scope, the scope 22 00:01:41,825 --> 00:01:46,682 of the whole program. So the way to think about that is that 23 00:01:46,682 --> 00:01:51,037 there's a box kind of around this whole program, and every definition in that box 24 00:01:51,037 --> 00:01:57,702 is recorded in the box. So in this global or top-level scope, we 25 00:01:57,702 --> 00:02:03,110 have recorded the fact that there's a definition for P. 26 00:02:03,110 --> 00:02:08,120 So there's a little rotation here that says, hey there's a P in this box. 27 00:02:08,120 --> 00:02:14,270 Then whenever we encounter a local, we make another box just around the local. 28 00:02:14,270 --> 00:02:20,055 And every definition that's in the local gets set up in that box, so whereas the 29 00:02:20,055 --> 00:02:25,751 top levels go up, the top level box just has p, this inner scope, this inner box 30 00:02:25,751 --> 00:02:32,300 has p and fetch. Okay. 31 00:02:32,300 --> 00:02:34,900 So that's the first half of Lexical scoping. 32 00:02:34,900 --> 00:02:39,736 The second half is what do we do whenever we see a reference to a constant or a 33 00:02:39,736 --> 00:02:44,180 function. Well, here's what we do, it's easy. 34 00:02:44,180 --> 00:02:49,900 We'd start right at the reference, and we'd go to the innermost enclosing box. 35 00:02:51,420 --> 00:02:56,070 And we'd see if that innermost enclosing box has a definition for that reference. 36 00:02:56,070 --> 00:03:01,150 So starting at this p. We go to the innermost enclosing box 37 00:03:01,150 --> 00:03:06,370 which is the box of that local and we say hey is P defined here? 38 00:03:06,370 --> 00:03:08,320 The answer is yes, it is. And that's it. 39 00:03:08,320 --> 00:03:10,372 We're done. We know which P this is. 40 00:03:10,372 --> 00:03:15,223 It's the P that we find in the innermost enclosing box that defines it So this 41 00:03:15,223 --> 00:03:20,370 thing here is the one from this box so it's accio. 42 00:03:20,370 --> 00:03:26,354 Okay, now, let's look at a little bit more complicated case. 43 00:03:26,354 --> 00:03:33,906 I'm just going to paste it in here. There's a little bit more complicated 44 00:03:33,906 --> 00:03:39,176 case. Well, what's this going to produce? 45 00:03:39,176 --> 00:03:44,400 Before we run it, let's try to use the scope contours to figure it out. 46 00:03:45,860 --> 00:03:51,044 Meaning the global scope there's going to be two definitions a and b, and it's 47 00:03:51,044 --> 00:03:56,179 these definitions here where a is 1 and b is 2. 48 00:03:56,179 --> 00:04:01,913 And way down inside this local, there's going to be one definition for b. 49 00:04:01,913 --> 00:04:04,360 In this definition, b is 3. So now, let's go look at all the 50 00:04:04,360 --> 00:04:13,900 references. What are they each going to be? 51 00:04:13,900 --> 00:04:18,030 Well, this first a, we start at the reference a and we go to the nearest in 52 00:04:18,030 --> 00:04:22,230 closing box, which in this case is the global scope and we say is there an a 53 00:04:22,230 --> 00:04:27,150 there? Yes there is, there's an a here and so 54 00:04:27,150 --> 00:04:31,308 that I would, would go to this a here, let's just draw a little arrow that says 55 00:04:31,308 --> 00:04:37,448 if that a is that a. That reference to a goes to this 56 00:04:37,448 --> 00:04:41,954 definition of a. Okay, now, let's find the next reference 57 00:04:41,954 --> 00:04:45,440 in this program. Well, here it is here after the plus, 58 00:04:45,440 --> 00:04:49,790 it's this a right here. Now, which a is that? 59 00:04:49,790 --> 00:04:53,484 We don't know. Let's go to the nearest enclosing scope 60 00:04:53,484 --> 00:04:57,750 contour. It's this innter box. 61 00:04:57,750 --> 00:05:00,845 Does it define an a? No, it doesn't. 62 00:05:00,845 --> 00:05:03,785 Let's go to the next nearest enclosing box. 63 00:05:03,785 --> 00:05:10,690 It's the outer box, does it define an a? Yes it does. 64 00:05:10,690 --> 00:05:15,289 So this referenced a here goes to the global definition of a there, where the 65 00:05:15,289 --> 00:05:19,600 top level definition of a there. Okay. 66 00:05:19,600 --> 00:05:27,383 What about this b? Well, we do the same thing, we start at 67 00:05:27,383 --> 00:05:31,037 the b, we go to the innermost box, we say, does this box define a b, yes it 68 00:05:31,037 --> 00:05:35,975 does, so this b goes to the inner definition of b. 69 00:05:35,975 --> 00:05:43,356 Let's go to the next reference. Now, let's be careful, this b here, is 70 00:05:43,356 --> 00:05:49,666 outside of the local. So when we go to look at the nearest and 71 00:05:49,666 --> 00:05:56,140 closing box, it's the global scope. So which b did this end up being? 72 00:05:56,140 --> 00:06:03,552 It ends up being the global b. So with these arrows on this, you could 73 00:06:03,552 --> 00:06:12,260 kind of see that, let's see, this is going to be plus 1, also 1, 3 and 2. 74 00:06:12,260 --> 00:06:17,290 1, 1, 3, and 2 is, I think, 7. Sure enough. 75 00:06:17,290 --> 00:06:24,547 Let me suggest that you do some exercises, I'm about to give you some, to 76 00:06:24,547 --> 00:06:31,872 do with the scope contours. And you can kind of check your work 77 00:06:31,872 --> 00:06:35,330 automatically with Dr. Racket but please, please, please, 78 00:06:35,330 --> 00:06:39,051 please, please, please, please, please,please, let me encourage you to 79 00:06:39,051 --> 00:06:43,077 first try to solve these problems on your own before you use this trick I'm about 80 00:06:43,077 --> 00:06:48,880 to show you to check your work. In Dr. 81 00:06:48,880 --> 00:06:54,088 Racket in the intermediate student language when you've got locals If you 82 00:06:54,088 --> 00:06:59,464 click on check syntax, and then you hover over a reference, it will draw a line 83 00:06:59,464 --> 00:07:07,671 from the corresponding definition. If you hover over a definition, it will 84 00:07:07,671 --> 00:07:12,575 draw lines to all the corresponding references. 85 00:07:12,575 --> 00:07:17,240 Okay? So that b is that b. 86 00:07:20,250 --> 00:07:22,220 Right? So you can check your work this way. 87 00:07:22,220 --> 00:07:26,808 It doesn't draw the boxes, it doesn't draw the boxes, but it does draw a line 88 00:07:26,808 --> 00:07:31,666 showing you which a it is. So you can use that to check your work, 89 00:07:31,666 --> 00:07:35,586 but please, please, please practice some of this scope contour stuff without using 90 00:07:35,586 --> 00:07:39,800 that. It's really important to understand the 91 00:07:39,800 --> 00:07:43,595 idea of lexical scoping without using an automatic tool to answer the question for 92 00:07:43,595 --> 00:07:45,030 you.