1 00:00:00,001 --> 00:00:05,657 In this short video, I want to show you how to use the stepper, which is 2 00:00:05,657 --> 00:00:12,626 functionality built into DrRacket that can help you understand the step-by-step 3 00:00:12,626 --> 00:00:20,647 evaluation of complex expressions. I have the stepper-starter.rkt file open 4 00:00:20,647 --> 00:00:23,707 here, and if we look at this file, there's one expression here athe top, a 5 00:00:23,707 --> 00:00:28,930 simple expression, and then a function definition and a call to that function. 6 00:00:28,930 --> 00:00:35,365 If we look at this expression, let's see, plus times 3 2, that's 6 plus 1, that's 7 00:00:35,365 --> 00:00:42,507 7, that's easy enough to see. And here we're calling max-dim with a 8 00:00:42,507 --> 00:00:45,625 rectangle. And there's an if, and it may be a little 9 00:00:45,625 --> 00:00:49,410 bit harder to quickly see what the result is here. 10 00:00:49,410 --> 00:00:52,398 Let's run it. Let's see that produced seven and the 11 00:00:52,398 --> 00:00:56,818 call to the function produced 20, and you could try to work through this and see 12 00:00:56,818 --> 00:01:00,880 why it's 20. And maybe you can see that quickly. 13 00:01:00,880 --> 00:01:03,840 Or maybe you can't. What the step is for is to help you 14 00:01:03,840 --> 00:01:08,088 understand how the evaluation re-expression [/g] produced a certain 15 00:01:08,088 --> 00:01:11,623 value. In cases where it's too complicated to 16 00:01:11,623 --> 00:01:15,992 just see. So let's try writing the stepper. 17 00:01:15,992 --> 00:01:20,975 I'll say step here. And I'll make this window A little bit 18 00:01:20,975 --> 00:01:24,788 bigger. And what the stepper does is it goes 19 00:01:24,788 --> 00:01:29,872 through the file and it follows the beginnings to the language evaluation 20 00:01:29,872 --> 00:01:35,130 rules. So the require doesn't produce a value. 21 00:01:35,130 --> 00:01:39,006 The require is just a declaration, so, on the left-hand side, we see What brackets 22 00:01:39,006 --> 00:01:42,768 evaluating on the right hand side, we see the current, the result of the current 23 00:01:42,768 --> 00:01:48,765 evaluation step. So let's see, try to evaluate plus, open 24 00:01:48,765 --> 00:01:57,155 paren plus, but the first operand is not a value, so it has to be evaluated. 25 00:01:57,155 --> 00:02:00,829 Open param times. Both operands are values so this 26 00:02:00,829 --> 00:02:05,061 expression can now be evaluated. That's what's racket [/g] is telling us 27 00:02:05,061 --> 00:02:07,984 in green. Is that this expression is ready to be 28 00:02:07,984 --> 00:02:11,930 evaluated. And it produces six. 29 00:02:11,930 --> 00:02:17,440 If I say step again, then what's on the right here Moves to the left. 30 00:02:17,440 --> 00:02:20,968 And in green, Racket is telling us okay, this whole expression is ready to be 31 00:02:20,968 --> 00:02:25,684 evaluated. All of the operands are already values 32 00:02:25,684 --> 00:02:32,949 and when I call plus on 6 and 1 I get 7. If I say Step again, now Racket says, hey 33 00:02:32,949 --> 00:02:36,594 it's. Seeing this function definition and 34 00:02:36,594 --> 00:02:40,921 that's just a definition nothing happens to it. 35 00:02:40,921 --> 00:02:44,460 Its also seen a call to that function max dim. 36 00:02:44,460 --> 00:02:47,484 The call isn't ready to be evaluated yet because one of the operands the only 37 00:02:47,484 --> 00:02:52,762 operand is not yet a value. But that operand itself is ready to be 38 00:02:52,762 --> 00:03:00,042 evaluated because all of its operands are values and when it evaluates rectangle of 39 00:03:00,042 --> 00:03:07,092 10, 20 solid blue. The result produced by that is the 40 00:03:07,092 --> 00:03:10,168 rectangle. It's, it doesn't quite look blue here, 41 00:03:10,168 --> 00:03:14,900 because Racket is highlighting it for us. But the call to rectangle with those 42 00:03:14,900 --> 00:03:18,744 arguments, produces a blue rectangle, so now over here on the left, we have 43 00:03:18,744 --> 00:03:23,230 max-dim of the blue rectangle. Again, it doesn't quite look blue because 44 00:03:23,230 --> 00:03:27,020 of the green highlight. [INAUDIBLE] And what's the rule for 45 00:03:27,020 --> 00:03:29,530 calling a function? Well the rule for calling a function is 46 00:03:29,530 --> 00:03:32,885 to replace the call to the function by the body of the function, this stuff 47 00:03:32,885 --> 00:03:37,515 here. Where every occurrence of the parameter, 48 00:03:37,515 --> 00:03:40,925 in this case, IMG, has been replaced by the argument, in this case, the blue 49 00:03:40,925 --> 00:03:44,528 rectangle. So that's what we've got here, one 50 00:03:44,528 --> 00:03:50,281 evaluation step takes to max dim of the blue rectangle to this whole expression. 51 00:03:50,281 --> 00:03:56,500 If we say step again, now Racket is trying to evaluate the if Expression. 52 00:03:56,500 --> 00:04:00,196 It can't do that, because the question is not yet a value, so it wants to evaluate 53 00:04:00,196 --> 00:04:03,588 the greater than. It can't do that because the first 54 00:04:03,588 --> 00:04:07,442 operand is not yet a value. It wants to evaluate the call to image 55 00:04:07,442 --> 00:04:09,713 width. It can do that because the first operand 56 00:04:09,713 --> 00:04:17,852 is a value that produces 10. Similarly [/g] now we can do the call to 57 00:04:17,852 --> 00:04:21,670 image height. Which produces 20. 58 00:04:21,670 --> 00:04:26,330 Now it can do the greater than 10 20 because both operands are values. 59 00:04:26,330 --> 00:04:30,857 That produces false. Now the rule for an If is that if the 60 00:04:30,857 --> 00:04:36,591 question is the value false, then the entire If gets replaced by the false 61 00:04:36,591 --> 00:04:42,320 answer expression. That's what's happening here, here's the 62 00:04:42,320 --> 00:04:45,818 false answer expression. And the entire if gets replaced with that 63 00:04:45,818 --> 00:04:49,170 here. Now Racket can evaluate this call to 64 00:04:49,170 --> 00:04:53,800 image height because the operand is a value. 65 00:04:53,800 --> 00:04:57,215 It now produces 20 and that's the end of the evaluation. 66 00:04:57,215 --> 00:05:03,780 You can also go backwards if you want to, just to go back to some earlier point. 67 00:05:03,780 --> 00:05:08,320 So that's how the stepper works. And what the stepper is always showing 68 00:05:08,320 --> 00:05:12,722 you is its always showing you whats the innermost expression where all the 69 00:05:12,722 --> 00:05:17,774 operands Our values. And on the right it's showing you, 70 00:05:17,774 --> 00:05:24,479 highlighted in pink, or purple I guess, what the, value of that expression is. 71 00:05:24,479 --> 00:05:29,020 And you can step backwards and forwards to understand this. 72 00:05:29,020 --> 00:05:30,839 I'd encourage you to learn how to use the stepper. 73 00:05:30,839 --> 00:05:33,971 As our programs get more and more complicated, the stepper can be very 74 00:05:33,971 --> 00:05:37,373 useful, particularly if, for example, when you have a function which isn't 75 00:05:37,373 --> 00:05:41,860 producing the value that you wanted it to. 76 00:05:41,860 --> 00:05:47,480 You can then step through it and see why its producing the value that it does.