1 00:00:06,510 --> 00:00:09,750 In the last video, we learned how to write expressions that operate on 2 00:00:09,750 --> 00:00:12,480 numbers. That video should've given you a pretty 3 00:00:12,480 --> 00:00:14,060 good sense of how to write those expressions. 4 00:00:15,680 --> 00:00:19,830 In this video, we're going to look in more detail at the rules Racket uses to 5 00:00:19,830 --> 00:00:24,510 evaluate those expressions. Now, you may find it a little pedantic to 6 00:00:24,510 --> 00:00:30,180 talk about the precise rules for evaluating an expression like plus 1, 2. 7 00:00:30,180 --> 00:00:32,690 And if all programs were that short, you'd be right. 8 00:00:34,050 --> 00:00:39,030 But here's something to consider. The Chevy Volt has 10 million lines of 9 00:00:39,030 --> 00:00:42,760 source code in it. 10 million lines of source code in a car. 10 00:00:42,760 --> 00:00:46,840 So, programs get quite big. If that was a program written in the 11 00:00:46,840 --> 00:00:50,660 beginning student language, it would have many more than 10 million expressions in 12 00:00:50,660 --> 00:00:53,660 it. And it turns out that in order to be able 13 00:00:53,660 --> 00:00:58,059 to understand big programs, we really need to understand the detailed rules by 14 00:00:58,059 --> 00:01:02,270 which expressions get evaluated. We won't always need to think in terms of 15 00:01:02,270 --> 00:01:05,450 those rules, but we do need to have them to fall back on. 16 00:01:06,630 --> 00:01:08,370 So, that's what we're going to look at in this video. 17 00:01:10,180 --> 00:01:14,340 Here's the expression that I'd like to work with, and if I just run this I'll 18 00:01:14,340 --> 00:01:18,940 get the value 14. What I want to do though is look at the 19 00:01:18,940 --> 00:01:21,880 detailed step by step process that Racket uses. 20 00:01:21,880 --> 00:01:24,680 When it evaluates this expression that results in 14. 21 00:01:24,680 --> 00:01:31,848 Before I do that, I want to introduce an additional bit of terminology here. 22 00:01:31,848 --> 00:01:39,810 We're going to say that an expression, like this one, because it starts with an 23 00:01:39,810 --> 00:01:43,040 open parenthesis, and then the name of a primitive operation. 24 00:01:43,040 --> 00:01:45,460 It's called a primitive call or a call to a primitive. 25 00:01:45,460 --> 00:01:52,675 And in a primitive call, we're going to say that this is the operator in a 26 00:01:52,675 --> 00:01:57,620 primitive call. We're going to say that all of these 27 00:01:57,620 --> 00:02:01,985 expressions that follow the operator in this case there's three of them are the 28 00:02:01,985 --> 00:02:07,370 operands. And, of course, we could do this same 29 00:02:07,370 --> 00:02:10,505 thing because there's another primitive call sitting right here. 30 00:02:10,505 --> 00:02:16,510 And inside that primitive call, that's the operator and those are the operands. 31 00:02:16,510 --> 00:02:24,450 And I could do the same thing over here. So, that's a bit of terminology. 32 00:02:24,450 --> 00:02:26,990 Now, let's look at the step by step process. 33 00:02:29,029 --> 00:02:34,490 That takes that expression and ends up producing the value 14. 34 00:02:34,490 --> 00:02:37,150 Well, the first thing that happens is Racket is asked to evaluate this entire 35 00:02:37,150 --> 00:02:40,480 expression. And it sees that it's a primitive call, 36 00:02:40,480 --> 00:02:45,255 because it starts with a open parenthesis and A primitive operator. 37 00:02:45,255 --> 00:02:50,850 So, the rule for evaluating a primitive call is that first all of the operands 38 00:02:50,850 --> 00:02:56,860 need to be reduced to values. Well this operand, the first one already 39 00:02:56,860 --> 00:03:00,039 is a value. So, no evaluation work's going to be 40 00:03:00,039 --> 00:03:05,510 required if it's already a value. But this operand is an expression, it's 41 00:03:05,510 --> 00:03:09,440 not a value. So, some work is going to be required 42 00:03:09,440 --> 00:03:10,890 there. Let's look at that. 43 00:03:10,890 --> 00:03:14,816 Well, this whole thing is a primitive call because it starts with a parenthesis 44 00:03:14,816 --> 00:03:20,340 and a primitive operator. So, let's look at its operands. 45 00:03:20,340 --> 00:03:29,181 Well, that first operand is already a value, so no work required there. 46 00:03:29,181 --> 00:03:30,202 And that second operand is already a value so no work required there. 47 00:03:30,202 --> 00:03:36,690 So, at this point, the two operands in this primitive call have been reduced to 48 00:03:36,690 --> 00:03:40,658 values, and so this multiplication can happen. 49 00:03:40,658 --> 00:03:46,400 And this whole thing is just going to become 12. 50 00:03:46,400 --> 00:03:48,952 So, it would say that in the first step of the evaluation. 51 00:03:48,952 --> 00:03:53,953 This whole expre, this whole expression becomes this. 52 00:03:53,953 --> 00:04:02,770 Now, I've left some extra space in here that you wouldn't normally leave here 53 00:04:02,770 --> 00:04:06,060 just to make things continue to line up. But here, we see that in the first 54 00:04:06,060 --> 00:04:12,360 evaluation step that operand get reduced to the value 12. 55 00:04:12,360 --> 00:04:16,490 Well, now we've reduced the first two operands to plus to values, we need to 56 00:04:16,490 --> 00:04:22,680 work on this third one. It's an expression now, it's not a value 57 00:04:22,680 --> 00:04:25,225 so we need to work on it. Let's see. 58 00:04:25,225 --> 00:04:34,950 Open paren, name of primitive operation. This first operand here is not a value. 59 00:04:34,950 --> 00:04:37,223 So, we need to reduce it. Let's see. 60 00:04:37,223 --> 00:04:39,690 Open paren, the name of primitive operation. 61 00:04:41,260 --> 00:04:46,150 This operand is a value, this operand is a value so this point this thing here. 62 00:04:51,020 --> 00:04:56,543 We can do the plus to get three. So, the next step is evaluation is this. 63 00:04:56,543 --> 00:05:02,525 So that plus 1, 2 has now become 3. So again, we were working on these 64 00:05:02,525 --> 00:05:05,460 operands to minus, this one's a value now. 65 00:05:05,460 --> 00:05:11,950 This one already is a value, so this minus can happen to get zero. 66 00:05:11,950 --> 00:05:19,850 At this point, all the operands for that plus have been reduced to values. 67 00:05:19,850 --> 00:05:24,855 So, that plus itself can happen. And the whole thing, of course, becomes 68 00:05:24,855 --> 00:05:29,805 14. We'll learn more about the detailed 69 00:05:29,805 --> 00:05:32,910 step-by-step evaluation rules in a couple of more videos. 70 00:05:34,380 --> 00:05:38,690 But for now, the intuition to take away from this is that the evaluation of an 71 00:05:38,690 --> 00:05:46,030 expression in general proceeds from left to right and from inside to outside. 72 00:05:46,030 --> 00:05:52,390 So, as we evaluate the outer plus, this plus 3, 4, this times 3, 4 rather happens 73 00:05:52,390 --> 00:05:57,030 first because we go left to right. But then when we try to do this whole 74 00:05:57,030 --> 00:06:07,250 expression, this plus 1, 2 has to happen first, because of the inside outside 75 00:06:07,250 --> 00:06:11,320 behavior. Because all of the operands to that minus 76 00:06:11,320 --> 00:06:13,990 have to become values before the minus can proceed. 77 00:06:13,990 --> 00:06:20,300 That left to right inside to outside intuition is an important way to 78 00:06:20,300 --> 00:06:23,580 understand the evaluation of expressions in the beginning student language. 79 00:06:25,750 --> 00:06:30,280 Now, you've seen the first evaluation rule, the primitive call rule. 80 00:06:30,280 --> 00:06:34,040 And you've seen how repeated application of that rule can evaluate numerical 81 00:06:34,040 --> 00:06:39,000 expressions of arbitrary complexity. You've also seen how repeated application 82 00:06:39,000 --> 00:06:43,470 of that rule leads to a left to right inside to outside evaluation order. 83 00:06:45,910 --> 00:06:48,650 One of the great things about our beginning student language is that it 84 00:06:48,650 --> 00:06:51,248 actually turns out to now have very many evaluation rules. 85 00:06:51,248 --> 00:06:55,680 We're going to learn three more in the first week of the course. 86 00:06:55,680 --> 00:06:58,886 And then, just three more the entire rest of the course. 87 00:06:58,886 --> 00:07:02,770 And that's going to let us write programs of arbitrary complexity. 88 00:07:04,090 --> 00:07:08,640 That's one of the reasons why we're using the BSL language, it lets us spend not 89 00:07:08,640 --> 00:07:13,660 very much time explaining the language. So, we can focus more of our time on how 90 00:07:13,660 --> 00:07:17,860 to design programs. And that, the ability to design programs 91 00:07:17,860 --> 00:07:22,572 in any language, is really the most important thing to learn.