1 00:00:06,220 --> 00:00:09,190 In this first video on how local works, I'm going to give you the basic 2 00:00:09,190 --> 00:00:13,528 intuitions for local expressions. And also show you how to write well 3 00:00:13,528 --> 00:00:19,090 formed local expressions. Well, it's graduation time once again. 4 00:00:19,090 --> 00:00:21,980 We're going to move on to a higher level learning language. 5 00:00:23,000 --> 00:00:27,340 So as before, I go down to the lower corner here, and I click. 6 00:00:27,340 --> 00:00:29,780 And I get a menu that that you can't see right now. 7 00:00:31,790 --> 00:00:35,999 I have to click on Choose Language > Intermediate Student under the How to 8 00:00:35,999 --> 00:00:42,052 Design Programs Languages. So now I'm an Intermediate Student and 9 00:00:42,052 --> 00:00:45,878 it's yellow because I haven't run yet. As soon as I run it won't be yellow 10 00:00:45,878 --> 00:00:48,779 anymore. So what we're going to do now, the reason 11 00:00:48,779 --> 00:00:52,937 we switched to this new language is to get a new language consturct called local 12 00:00:52,937 --> 00:00:59,599 and our presentation of local is going to be broken up over several short videos. 13 00:01:00,600 --> 00:01:05,670 I'm sure you'll be glad to get a short video after some of last week's videos. 14 00:01:05,670 --> 00:01:08,096 So here's an example of using local. I won't type the thing, the whole thing 15 00:01:08,096 --> 00:01:09,850 out. I'll just put it here for you to see. 16 00:01:09,850 --> 00:01:14,170 And in a minute I'll talk about the rules for forming local. 17 00:01:14,170 --> 00:01:17,265 So I'll more precisely talk about the different parts of the local. 18 00:01:17,265 --> 00:01:21,360 But intuitively for now, the local has two parts. 19 00:01:21,360 --> 00:01:24,810 It has the definitions which are here. And the body, which is here. 20 00:01:25,880 --> 00:01:28,320 And the way to think about the local, is, it says, hey. 21 00:01:28,320 --> 00:01:33,250 These definitions, they're valid only inside the local. 22 00:01:33,250 --> 00:01:37,526 They're not valid anywhere else. So if I run this program, you shouldn't 23 00:01:37,526 --> 00:01:43,048 be too surprised to get the result as 3. Because this says hey, inside the local, 24 00:01:43,048 --> 00:01:46,683 a is 1, b is 2. And this thing called the body is an 25 00:01:46,683 --> 00:01:52,330 expression which is evaluated to produce the result of the local. 26 00:01:52,330 --> 00:01:58,820 So that's a local where all of the locally defined definitions are 27 00:01:58,820 --> 00:02:02,830 constants. Here's a local in which one of the 28 00:02:02,830 --> 00:02:06,180 locally defined definitions is a local constant, and I'm putting those in lower 29 00:02:06,180 --> 00:02:09,110 case. Case. 30 00:02:09,110 --> 00:02:13,440 In the other locally defined definition is a function definition. 31 00:02:13,440 --> 00:02:18,368 Local will let you have any definitions you want in this define part. 32 00:02:18,368 --> 00:02:23,290 You could even put in a define struct but we're not going to do that. 33 00:02:23,290 --> 00:02:28,320 So this says, hey there's this locally defined constant p, the value is accio. 34 00:02:28,320 --> 00:02:32,480 And hey, there's this locally defined function, and the body of that function 35 00:02:32,480 --> 00:02:36,712 happens to refer to p. But that's okay because p is defined as a 36 00:02:36,712 --> 00:02:43,551 constant inside this local. So, if I say, run this, then I get accio 37 00:02:43,551 --> 00:02:46,486 portkey. Okay? 38 00:02:46,486 --> 00:02:51,020 The way to think about is it's almost as if I had done this. 39 00:02:51,020 --> 00:02:54,145 Only almost as if its different in important ways. 40 00:02:54,145 --> 00:03:00,060 But it's almost as if I had done this, where the definitions were what's called 41 00:03:00,060 --> 00:03:04,254 at top level. They weren't inside a local. 42 00:03:04,254 --> 00:03:07,640 And I just did that. But it isn't that. 43 00:03:07,640 --> 00:03:11,930 It's quite different, and it's different in an important way, which is, these 44 00:03:11,930 --> 00:03:15,778 definitions are only valid inside the local. 45 00:03:15,778 --> 00:03:20,490 So in particular, if I try to use a out here, it says there is no a, because the 46 00:03:20,490 --> 00:03:27,915 a is only defined inside the Local. Or, if I try to use fetch out here, it's 47 00:03:27,915 --> 00:03:36,210 not valid out here, because it's only valid inside that Local. 48 00:03:36,210 --> 00:03:40,571 So that's the way in which its different. Now, in the next two videos, I'll talk 49 00:03:40,571 --> 00:03:43,851 more precisely about the exact way which its different. 50 00:03:43,851 --> 00:03:47,730 But let me now just talk more precisely about exactly how you form a local. 51 00:03:47,730 --> 00:03:53,830 We're going back now to the kinds of slides we saw way back in the first week 52 00:03:53,830 --> 00:03:57,291 of the course. Rule for forming a local is 53 00:03:57,291 --> 00:03:59,650 straight-forward. Local is an expression. 54 00:03:59,650 --> 00:04:05,530 So you can put it anywhere you would put an expression, is [UNKNOWN] local. 55 00:04:05,530 --> 00:04:08,634 And then there's a bracket. And by convention, we use a square 56 00:04:08,634 --> 00:04:12,090 bracket. And then there's a balancing square 57 00:04:12,090 --> 00:04:14,438 bracket. And in between those two brackets there 58 00:04:14,438 --> 00:04:19,510 can be any number of definitions. One, two, however many you want. 59 00:04:19,510 --> 00:04:24,064 There can even be zero definitions, but in that case it isn't very useful to have 60 00:04:24,064 --> 00:04:29,526 the local. So local, the definitions. 61 00:04:29,526 --> 00:04:33,315 And then there's a thing called the body, which is an expression. 62 00:04:33,315 --> 00:04:37,091 And the intuitive way for understanding the local is that it sets up the 63 00:04:37,091 --> 00:04:41,123 definitions, it sets up a special world that has these definitions and then 64 00:04:41,123 --> 00:04:45,750 evaluates the body and produces that result. 65 00:04:47,970 --> 00:04:50,870 So there you go. There's our initial intuitive 66 00:04:50,870 --> 00:04:56,165 understanding of local and how to form a local expression.