1 00:00:06,390 --> 00:00:10,200 As I mentioned before, programs are always changing. 2 00:00:10,200 --> 00:00:13,161 Or put it this way, if you're fortunate enough to design a program that other 3 00:00:13,161 --> 00:00:16,758 people really want to use, they're going to want you to change it. 4 00:00:16,758 --> 00:00:19,730 They're going to want you to make it better and better and better. 5 00:00:21,650 --> 00:00:25,249 In this video, what I'm going to do is make a change to the cat program, give it 6 00:00:25,249 --> 00:00:30,527 some better functionality. And what you're going to see is that that 7 00:00:30,527 --> 00:00:35,170 careful design that we did is going to help make that change easy to manage. 8 00:00:35,170 --> 00:00:38,875 What we'll do is we'll run the change right through the whole recipe, starting 9 00:00:38,875 --> 00:00:42,520 with the analysis and then going on to the code. 10 00:00:43,700 --> 00:00:47,792 We've got a cat program, and we can run it as we solve, by saying main of zero, 11 00:00:47,792 --> 00:00:52,020 where zero is the initial state of the world. 12 00:00:52,020 --> 00:00:54,555 But there's something about this cat program I don't really like, which is the 13 00:00:54,555 --> 00:00:58,380 cat is moving a little bit too slowly. I just don't have time to wait for it to 14 00:00:58,380 --> 00:01:02,628 get there. So, let's see about revising the program 15 00:01:02,628 --> 00:01:06,729 to make the cat move more quickly. As I mentioned in the last video, if we 16 00:01:06,729 --> 00:01:11,010 think about this program and we think about the analysis that we started with. 17 00:01:11,010 --> 00:01:13,986 One of the things we worked very hard to do, was to make sure there was a clear 18 00:01:13,986 --> 00:01:18,330 correspondence between the analysis and the program. 19 00:01:18,330 --> 00:01:20,850 So, the constants in the program correspond to what we identified in the 20 00:01:20,850 --> 00:01:25,240 analysis. The data definitions in the program 21 00:01:25,240 --> 00:01:30,350 represent the changing information that we identified in the analysis. 22 00:01:30,350 --> 00:01:36,590 The big bang in the program has the big bang options identified in the analysis. 23 00:01:36,590 --> 00:01:40,930 And the handler functions played their roles with respect to the changing 24 00:01:40,930 --> 00:01:45,093 information and the interaction with big-bang. 25 00:01:45,093 --> 00:01:49,188 The fact that we've kept this clear correspondence between the program and 26 00:01:49,188 --> 00:01:53,543 the analysis, means that we can use the analysis as a kind of abbreviation, where 27 00:01:53,543 --> 00:01:58,710 the technical term would be a model of the program. 28 00:01:58,710 --> 00:02:03,845 We can think about what we need to do to the program by starting doing it with the 29 00:02:03,845 --> 00:02:08,480 analysis. So, let's do that. 30 00:02:08,480 --> 00:02:13,190 The problem we have is that the cat's moving too slowly. 31 00:02:13,190 --> 00:02:17,474 Some sense, what happened here when I look back at the analysis is we can say, 32 00:02:17,474 --> 00:02:22,053 hey, the cat's speed was an unidentified constant. 33 00:02:22,053 --> 00:02:26,585 There is this constant called the cat's speed. 34 00:02:26,585 --> 00:02:31,100 The cat's moving one pixel per clock tick. 35 00:02:31,100 --> 00:02:35,654 If we identify that as a constant [so, if we add speed to the analysis here], then 36 00:02:35,654 --> 00:02:41,385 we're going to have to change that speed to something more than one. 37 00:02:41,385 --> 00:02:45,873 And if we use that speed over here in the on take handler function, then changing 38 00:02:45,873 --> 00:02:50,620 that constant value will change the speed of the cat. 39 00:02:50,620 --> 00:02:54,700 So, now that we have updated the analysis, let's just go through and do it 40 00:02:54,700 --> 00:03:00,238 to the program. Well, the first thing is, there needs to 41 00:03:00,238 --> 00:03:04,171 be a speed constant. So, I'll go right here and I'll add the 42 00:03:04,171 --> 00:03:08,668 constant speed. And one was too slow, so, let's say 43 00:03:08,668 --> 00:03:11,385 three. The great thing about it being constant 44 00:03:11,385 --> 00:03:15,400 like this is if we think later that three is not enough, we can change it again. 45 00:03:15,400 --> 00:03:19,048 So, I've checked off this part of the analysis, where I was supposed to add the 46 00:03:19,048 --> 00:03:23,648 speed constant. And in the notes I made on the analysis, 47 00:03:23,648 --> 00:03:29,230 it says I also need to go look at the on-tick handler. 48 00:03:29,230 --> 00:03:33,966 So, I scroll down here to big bang, and the on-tick handler is called advanced 49 00:03:33,966 --> 00:03:37,786 cat. And if this was a big program, then I'll 50 00:03:37,786 --> 00:03:42,361 show you a Racket has a tool I can use, which is I click Check syntax, I click 51 00:03:42,361 --> 00:03:49,700 right on Advance-cat, and I can jump to the definition of Advance-cat. 52 00:03:49,700 --> 00:03:52,330 Now, it wasn't a big program so, I didn't need that help. 53 00:03:52,330 --> 00:03:54,860 But if it was a big program that would help. 54 00:03:54,860 --> 00:03:58,456 Another way to do it is you could go right up here, and search through all the 55 00:03:58,456 --> 00:04:02,250 definitions. Jump right to advance-cat. 56 00:04:02,250 --> 00:04:05,734 Now, I'm going to quickly replay the how to design functions recipe with this new 57 00:04:05,734 --> 00:04:09,589 constant in mind. The signature doesn't change, the purpose 58 00:04:09,589 --> 00:04:14,044 changes. We're going to produce the next cat by 59 00:04:14,044 --> 00:04:23,360 advancing its speed pixels to the right. So, let's see. 60 00:04:23,360 --> 00:04:26,733 The stub didn't change. The check-expects changed. 61 00:04:26,733 --> 00:04:33,357 Now this is, instead of being advance-cat 3 produces 4, this should be advance-cat 62 00:04:33,357 --> 00:04:39,230 of 3, and you might be tempted to put a 6 here. 63 00:04:39,230 --> 00:04:44,700 But that's not what I'm going to put here, I'm going to put plus 3 speed. 64 00:04:44,700 --> 00:04:48,620 That way when I go change the speed constant, this check expect will 65 00:04:48,620 --> 00:04:53,572 automatically be the right thing. In this way, this check expect really 66 00:04:53,572 --> 00:04:57,067 corresponds to the purpose. It says, whatever the speed constant 67 00:04:57,067 --> 00:05:00,970 happens to be, that's how much we're supposed to advance the cat by. 68 00:05:00,970 --> 00:05:04,720 So, let's see, advance-cat the template doesn't change. 69 00:05:04,720 --> 00:05:08,210 Here's the body of the function. And this one just becomes speed. 70 00:05:10,300 --> 00:05:15,658 I can run my test pass, and now I can say, main of 0, and I have a speedier 71 00:05:15,658 --> 00:05:24,706 cat. So, the important thing to take away from 72 00:05:24,706 --> 00:05:28,050 this video is programs always change. I said that before: Programs always 73 00:05:28,050 --> 00:05:32,080 change, because people want them to be better all the time. 74 00:05:32,080 --> 00:05:36,192 And so, that means we want it to be easy to change programs. 75 00:05:36,192 --> 00:05:40,680 And in the case of these word programs where we have this analysis, working hard 76 00:05:40,680 --> 00:05:45,032 to make the structure of the program match the structure of the analysis made 77 00:05:45,032 --> 00:05:49,520 it really easy to go back and make this change, because the analysis became kind 78 00:05:49,520 --> 00:05:57,168 of a model of the program. And we could think about what we needed 79 00:05:57,168 --> 00:06:01,423 to do to the program. First at the model level; first, using 80 00:06:01,423 --> 00:06:06,046 this analysis picture, and then quickly run through the program, catching it up 81 00:06:06,046 --> 00:06:11,538 to the new analysis. This ability to work on programs by 82 00:06:11,538 --> 00:06:14,606 reasoning about them at the model level is one of the things that really 83 00:06:14,606 --> 00:06:19,998 separates program designers from people who write code that just happens to work. 84 00:06:19,998 --> 00:06:24,516 And we're going to talk about it a lot more in the course as we go forward.