1 00:00:07,150 --> 00:00:12,040 We've been learning about different kinds of values, numbers, strings, and images, 2 00:00:12,040 --> 00:00:14,310 and how to write expressions that operate on those values. 3 00:00:15,910 --> 00:00:18,940 In this video, we're going to look at something quite different. 4 00:00:18,940 --> 00:00:21,830 We're going to look at how to write constant definitions. 5 00:00:21,830 --> 00:00:26,200 To give names to values so that we can use those values in different parts of 6 00:00:26,200 --> 00:00:30,000 the program. Constant definitions are more than just a 7 00:00:30,000 --> 00:00:33,450 convenience. We're going to see later in the course 8 00:00:33,450 --> 00:00:37,880 that they help us make programs that are easy for other people to read and easy 9 00:00:37,880 --> 00:00:41,770 for other people to change. And that those two properties, 10 00:00:41,770 --> 00:00:45,540 readability and changeability, are two of the most important properties the program 11 00:00:45,540 --> 00:00:49,200 can have. But for now, we're just going to focus on 12 00:00:49,200 --> 00:00:52,820 the mechanism of constant definitions. So that you can learn how to write a 13 00:00:52,820 --> 00:00:56,970 constant definition to give names to a value that you use in other parts of the 14 00:00:56,970 --> 00:00:59,060 program. So. 15 00:00:59,060 --> 00:01:02,755 let's imagine that we're working on a program in which there's a simple 16 00:01:02,755 --> 00:01:07,855 interactive game running in a window. One thing we might want to do is give 17 00:01:07,855 --> 00:01:10,830 names to the width and height of the window. 18 00:01:10,830 --> 00:01:16,570 And the way we do that is we write open param define And then sone name, which we 19 00:01:16,570 --> 00:01:21,500 often put the names of constants in uppercase, like this. 20 00:01:21,500 --> 00:01:35,650 And we might say that the window is 400 pixels wide, and maybe the window is 600 21 00:01:35,650 --> 00:01:37,090 high. And that's to name constants. 22 00:01:37,090 --> 00:01:40,610 If we run this program just like this We don't get any values out, because 23 00:01:40,610 --> 00:01:43,100 defining a constant doesn't produce a value. 24 00:01:44,360 --> 00:01:47,930 But now what I can do is I can write an expression, like this, and what happens 25 00:01:47,930 --> 00:01:57,300 is when Dr. Racket evaluates this When it comes to 26 00:01:57,300 --> 00:02:03,651 evaluating the name of a defined constant it's just going to use the constant's 27 00:02:03,651 --> 00:02:07,139 value there. So this will become 400. 28 00:02:07,139 --> 00:02:13,897 So the evaluation steps here would be times width, which will become 400. 29 00:02:13,897 --> 00:02:20,764 That's the first evaluation [/g] step. The next evaluation step height. 30 00:02:20,764 --> 00:02:26,800 We'll valuate to 600 At this point, all the arguments to times are values, so now 31 00:02:26,800 --> 00:02:34,680 we have 240,000. So the way to form a constant definition 32 00:02:34,680 --> 00:02:41,130 is to have an open parenthesis or define a space and then a name. 33 00:02:41,130 --> 00:02:45,580 And names typically include letters of the alphabet in upper or lower case they 34 00:02:45,580 --> 00:02:50,740 can also include numerical digits and in the beginning student language they can 35 00:02:50,740 --> 00:02:55,380 include lots of other characters like exclimation mark and question mark and 36 00:02:55,380 --> 00:02:59,160 equals and things like that. Some languages are more restrictive about 37 00:02:59,160 --> 00:03:02,910 that, but the beginning of student language is fairly, fairly generous about 38 00:03:02,910 --> 00:03:05,550 that. You can use parentheses or quotes of any 39 00:03:05,550 --> 00:03:12,190 kind now. The rules for evaluating a constant 40 00:03:12,190 --> 00:03:17,870 definition are when Racket encounters the constant definition It evaluates the 41 00:03:17,870 --> 00:03:24,850 expression, and uses the resulting value as the value of the name constant going 42 00:03:24,850 --> 00:03:28,990 forward. When evaluation encounters a name 43 00:03:28,990 --> 00:03:35,110 constant, the name constant simply evaluates to The value that the define 44 00:03:35,110 --> 00:03:39,910 associated with it. Let's look at another example. 45 00:03:39,910 --> 00:03:46,737 We get rid of all this now. Suppose in this same program, in my game 46 00:03:46,737 --> 00:03:49,490 program. What we have is an image of a cat going 47 00:03:49,490 --> 00:03:53,450 back and forth across the screen. Well, I might want to give a name to that 48 00:03:53,450 --> 00:03:56,470 image that I can use in lots of places. So there's the cat. 49 00:03:56,470 --> 00:04:01,810 And what I'm going to do here is go to the second edition of the How to Design 50 00:04:01,810 --> 00:04:07,350 Programs book, which we use as the basis for the material in this Of course and 51 00:04:07,350 --> 00:04:09,640 they happen to have a little cat here that we're going to use. 52 00:04:09,640 --> 00:04:14,943 So I'm just going to go copy this image. And I'll come back over here and I'll 53 00:04:14,943 --> 00:04:20,769 just paste it right in here. And what's happening in Racket is 54 00:04:20,769 --> 00:04:25,650 remember that images is values And all values are expressions. 55 00:04:25,650 --> 00:04:29,990 So I can stick that image right there in the program, as an expression. 56 00:04:29,990 --> 00:04:32,300 [SOUND] Most languages won't let you do this. 57 00:04:32,300 --> 00:04:36,120 But it's quite convenient to be able to do it in, in DSL, so we're going to take 58 00:04:36,120 --> 00:04:38,704 advantage of that. So now I've got a cat. 59 00:04:38,704 --> 00:04:44,472 And I can write some expressions. So here rotate is a primitive, and this 60 00:04:44,472 --> 00:04:49,968 says rotate the cat by ten degrees to the left. 61 00:04:49,968 --> 00:04:52,342 And here's another expression that would rotate. 62 00:04:52,342 --> 00:04:58,230 The cat, rotate the cat by minus ten degrees, is what that one does. 63 00:04:58,230 --> 00:05:05,420 This will rotate the cat by ten degrees. If we run this now, we see, first 64 00:05:05,420 --> 00:05:08,669 expression produces the cat rotated this way, the second expression produces the 65 00:05:08,669 --> 00:05:14,441 cat Rotated that way, and, we might even want to give names to these, cause 66 00:05:14,441 --> 00:05:20,703 remember I said that we can have an expression here after the constant name. 67 00:05:20,703 --> 00:05:24,020 So now I've given names. [NOISE] To those two rotated cats. 68 00:05:24,020 --> 00:05:26,340 Oops. I can't use those names. 69 00:05:26,340 --> 00:05:29,840 I've got to use different names. You can only define a given constant name 70 00:05:29,840 --> 00:05:31,680 one time. That's why it's called a constant name. 71 00:05:31,680 --> 00:05:39,620 It can't change. So I'll call those the RCAT and the LCAT. 72 00:05:39,620 --> 00:05:42,070 And there they are. So here you see that what Racket is 73 00:05:42,070 --> 00:05:46,460 letting us do is. They define in the name and then in 74 00:05:46,460 --> 00:05:50,459 expression. That expression gets evaluated, and for 75 00:05:50,459 --> 00:05:56,050 evermore that value will be associated with the constant name. 76 00:05:56,050 --> 00:05:58,158 So we can use it in other places in the program. 77 00:05:58,158 --> 00:06:02,310 I have an exercise I'd like to ask you to do now. 78 00:06:02,310 --> 00:06:05,056 This will help you test your understanding of the material in this 79 00:06:05,056 --> 00:06:09,990 video. So, now you've seen how we can define 80 00:06:09,990 --> 00:06:15,222 constants in beginning student language. As I said at the beginning of this video, 81 00:06:15,222 --> 00:06:18,540 we're going to see more and more how important this is. 82 00:06:18,540 --> 00:06:22,300 Really this notion of defining name constants is going to have a big effect 83 00:06:22,300 --> 00:06:27,040 on making our programs comprehensible to other programmers and easy to change in 84 00:06:27,040 --> 00:06:31,360 the future. Let me take a minute now to talk about 85 00:06:31,360 --> 00:06:34,810 the pace of the course. If these videos have been going too 86 00:06:34,810 --> 00:06:38,000 slowly for you. If maybe you've already programmed, then 87 00:06:38,000 --> 00:06:42,310 I just need to ask you to be patient. I promise you we're going to get there. 88 00:06:43,810 --> 00:06:46,650 If on the other hand maybe you haven't programmed before. 89 00:06:46,650 --> 00:06:49,290 Some of you might find these videos going a bit too quickly. 90 00:06:49,290 --> 00:06:53,720 One thing we've learned at UBC is that for some students it takes a little bit 91 00:06:53,720 --> 00:06:57,970 longer to get the hang of this. If that's the case for you, if you feel 92 00:06:57,970 --> 00:07:01,480 like it's going a bit too quickly, let me make three suggestions. 93 00:07:02,510 --> 00:07:07,170 First, go back through the exercises that you had a hard time with and work them 94 00:07:07,170 --> 00:07:10,750 again. Second, you can watch that part of the 95 00:07:10,750 --> 00:07:14,890 video again. But most importantly let me suggest you 96 00:07:14,890 --> 00:07:18,880 go to the week one page. Look at the instructions for how to get 97 00:07:18,880 --> 00:07:24,000 help, and post a message asking for help. One of the things we've learned in the 98 00:07:24,000 --> 00:07:28,300 thousands of students that we've taught this material to is it really helps a lot 99 00:07:28,300 --> 00:07:33,290 to ask other people and discuss the issues that your having a hard time with. 100 00:07:33,290 --> 00:07:35,800 That's going to be a big part of this course going forward.