1 00:00:05,880 --> 00:00:08,546 So as I said in the introduction, we're going to design some great programs in 2 00:00:08,546 --> 00:00:12,569 this course. Some games, some animations, some 3 00:00:12,569 --> 00:00:16,129 information visualization, some web programs. 4 00:00:16,129 --> 00:00:20,168 Really great stuff. But the thing is, you gotta walk before 5 00:00:20,168 --> 00:00:22,909 you can run. And in the same way, we've gotta learn 6 00:00:22,909 --> 00:00:27,080 some basic building blocks, before we can build these great programs. 7 00:00:28,310 --> 00:00:30,890 So that's what we're going to do for the next few videos, is learn some basic 8 00:00:30,890 --> 00:00:34,300 building blocks out of which we're going to build bigger programs. 9 00:00:35,660 --> 00:00:38,630 We're going to try to do it quickly, because it isn't that exciting. 10 00:00:38,630 --> 00:00:41,854 But we're going to try not to do it too quickly, because we want everybody to be 11 00:00:41,854 --> 00:00:45,870 able to stay with us. So, if maybe you've programmed before, 12 00:00:45,870 --> 00:00:50,270 and this video seems a little slow, then feel free to speed it up. 13 00:00:51,410 --> 00:00:54,010 But most of the people who take this class haven't programmed before, so we're 14 00:00:54,010 --> 00:00:57,599 going to try to go a good speed for that. And i, and if it is a little too fast, 15 00:00:57,599 --> 00:01:01,180 then you can replay the part of the video that you need to replay. 16 00:01:02,800 --> 00:01:06,336 As we go through it, I'd encourage you to have DrRacket open, and follow along with 17 00:01:06,336 --> 00:01:09,415 the examples that I'm going to do. So here we go. 18 00:01:09,415 --> 00:01:14,151 When we start Racket for the first time, we have to tell it what language we're 19 00:01:14,151 --> 00:01:19,524 going to use. Your Racket may have started up already 20 00:01:19,524 --> 00:01:23,030 saying beginning student language down in this lower left corner. 21 00:01:23,030 --> 00:01:25,900 If it did, then you're all set, you don't need to do anything. 22 00:01:25,900 --> 00:01:30,590 But if it didn't, then go to the Language menu. 23 00:01:30,590 --> 00:01:35,183 Say Choose Language. Make sure the How to Design Programs part 24 00:01:35,183 --> 00:01:40,187 is opened up, and choose Beginning Student. 25 00:01:40,187 --> 00:01:43,355 The way you'll get to this menu will be slightly different in windows of course, 26 00:01:43,355 --> 00:01:47,080 but your going to want to go to the Language menu to do that. 27 00:01:47,080 --> 00:01:50,472 So go to the Language menu, and select Beginning Student Language. 28 00:01:50,472 --> 00:01:53,992 And then you'll be all set to go. Now that, that's done, we can get 29 00:01:53,992 --> 00:01:57,302 started. The top part of Racket here is called the 30 00:01:57,302 --> 00:02:03,140 definitions area, and the bottom part is called the interaction area. 31 00:02:03,140 --> 00:02:05,855 We're going to start by working up in the definitions area. 32 00:02:05,855 --> 00:02:09,470 And we're going to start by writing a simple arithmetic expression. 33 00:02:09,470 --> 00:02:15,394 I'll just put plus 3 4. This is how we're going to say to add 3 34 00:02:15,394 --> 00:02:20,417 to 4, in the beginning student language. And if I ask DrRacket to run that short 35 00:02:20,417 --> 00:02:28,330 program, it will, and down here it tells me that the result of that program is 7. 36 00:02:28,330 --> 00:02:31,622 Which isn't so surprising, adding 3 to 4 should produce 7. 37 00:02:31,622 --> 00:02:36,536 This is what's called an expression, and in the bottom window we have what's 38 00:02:36,536 --> 00:02:40,458 called a value. And the way Racket is working, is we give 39 00:02:40,458 --> 00:02:44,105 it expressions, and it evaluates the expression to give it the value. 40 00:02:44,105 --> 00:02:51,533 Expressions can be more complicated, for example, we can say plus 3 times 2 3. 41 00:02:51,533 --> 00:02:57,383 And we can run that program. And most surprised, it produces 9. 42 00:02:57,383 --> 00:03:03,753 We can make expressions that are even more complicated than that, or use other 43 00:03:03,753 --> 00:03:11,055 primitive operators. Here we'll just divide 12 by times 2, 3. 44 00:03:11,055 --> 00:03:18,186 And unsurprisingly, that will get us 2. So what we've seen so far, is that the 45 00:03:18,186 --> 00:03:24,850 rule for the way we form an expression is open parenthesis, the name of a primitive 46 00:03:24,850 --> 00:03:33,579 operator like plus, or times, or slash. And then any number of other expressions, 47 00:03:33,579 --> 00:03:37,710 followed by a closed parentheses. And there's another rule that says 48 00:03:37,710 --> 00:03:41,842 expressions can be actual values, so numbers themselves can be expressions. 49 00:03:41,842 --> 00:03:48,460 Let me show you another thing we could do. 50 00:03:48,460 --> 00:03:51,896 We could take all of this. And if we say to Racket, comment that out 51 00:03:51,896 --> 00:03:54,879 with semicolon. And then we'll put a semi colon in front 52 00:03:54,879 --> 00:03:58,207 of each of those lines, and what that tells Racket is, for now it should ignore 53 00:03:58,207 --> 00:04:02,061 everything on a line after the first semi colon. 54 00:04:02,061 --> 00:04:05,111 So if I would run this program now, as far as Racket's concerned there's no 55 00:04:05,111 --> 00:04:09,323 expressions in there at all, and so there's no values that come out. 56 00:04:09,323 --> 00:04:14,616 Let me tell you about two more primitive operations or numbers, and then I'll ask 57 00:04:14,616 --> 00:04:20,720 you to do an exercise. The first one is sqr, we call it square. 58 00:04:20,720 --> 00:04:26,920 And if I take square of 3, and I'll show you another one here at the same time, 59 00:04:26,920 --> 00:04:33,408 the square root of 16, and I'll run these two. 60 00:04:33,408 --> 00:04:40,352 And what you're seeing is that square, squares the number, and square root 61 00:04:40,352 --> 00:04:49,020 produces the square root of the number. Okay, we've seen how to form expressions, 62 00:04:49,020 --> 00:04:54,168 and we've seen a number of primitives that operate on numbers, primitives like 63 00:04:54,168 --> 00:05:00,884 plus and times and divide and minus, and square and square root. 64 00:05:00,884 --> 00:05:04,239 What I like to do now, is give you an exercise that you can use to help 65 00:05:04,239 --> 00:05:09,516 reinforce what we've learned so far. Well the exercises is like this 66 00:05:09,516 --> 00:05:12,820 throughout the videos in this course. I'd like to encourage you to do them, 67 00:05:12,820 --> 00:05:16,059 just to test your understanding of what's come so far. 68 00:05:16,059 --> 00:05:19,982 So here's the exercise. I've got it in a separate file, and let 69 00:05:19,982 --> 00:05:24,847 me just explain this big box. Racket has a thing called a comment box, 70 00:05:24,847 --> 00:05:28,650 which you can actually add to a file yourself, under the Insert menu. 71 00:05:28,650 --> 00:05:32,330 And the way to think of a comment box, is, see this semicolon here? 72 00:05:32,330 --> 00:05:35,485 It's basically saying everything in the box is a comment. 73 00:05:35,485 --> 00:05:38,957 And what's also need about Racket is that you can put images in the middle of 74 00:05:38,957 --> 00:05:43,874 files. That just an image that I Cut and Pasted. 75 00:05:43,874 --> 00:05:48,914 And anyway, here's the exercise. Go ahead and work on it. 76 00:05:48,914 --> 00:05:54,630 Write the expression. And when you're done, restart the video. 77 00:05:54,630 --> 00:05:58,621 I hope you enjoyed doing that exercise. Let me just quickly show you how I think 78 00:05:58,621 --> 00:06:04,470 about it. Given the formula, I know that I need to 79 00:06:04,470 --> 00:06:12,600 square 3, and I need to square 4. And I need to add those together. 80 00:06:12,600 --> 00:06:18,261 And I need to take the square root of that whole thing. 81 00:06:18,261 --> 00:06:26,734 And if I run that, I get 5. Sure enough. 82 00:06:26,734 --> 00:06:32,574 Let me say a word here about math. The Pythagorean theor, theorem here, is 83 00:06:32,574 --> 00:06:37,540 pretty much the hardest math we're going to do in this whole course. 84 00:06:37,540 --> 00:06:40,642 And that's important, because you need to know that to design a lot of programs, 85 00:06:40,642 --> 00:06:45,094 you don't need to know a lot of math. You can be a very good program designer, 86 00:06:45,094 --> 00:06:48,951 without knowing a lot of math. There are certain types of programs, 87 00:06:48,951 --> 00:06:52,368 programs in graphics or vision or machine learning, where you do need to l, know a 88 00:06:52,368 --> 00:06:55,524 lot of math. That's because you need to understand 89 00:06:55,524 --> 00:06:58,832 that domain. We won't do a lot of math in this course, 90 00:06:58,832 --> 00:07:02,550 though. The Pythagorean theorem here is about the 91 00:07:02,550 --> 00:07:06,395 hardest we'll do. There is, however, one little detail that 92 00:07:06,395 --> 00:07:09,806 I need to tell you. A little bit more math, and then we'll go 93 00:07:09,806 --> 00:07:14,606 on. [SOUND] If I take the square root of 2, 94 00:07:14,606 --> 00:07:19,136 and run that. Look at this funny thing Racket is 95 00:07:19,136 --> 00:07:23,070 telling me that I see. Why is that? 96 00:07:24,260 --> 00:07:27,109 Well you may know that the square root of 2 is a irrational number. 97 00:07:27,109 --> 00:07:30,196 And what it means to be an irrational number, is that it takes infinite space 98 00:07:30,196 --> 00:07:36,260 to write the number down. Well, computers are not infinite in size. 99 00:07:36,260 --> 00:07:39,335 They're quite finite in size. So it has no way of representing a 100 00:07:39,335 --> 00:07:42,010 number, that takes infinite space to write it down. 101 00:07:42,010 --> 00:07:47,470 So what this means, this sharp sign I and a number means, it's an inexact number. 102 00:07:47,470 --> 00:07:51,250 It's telling you that the number is pretty close to this number here, but not 103 00:07:51,250 --> 00:07:54,395 exactly. And you may see numbers like that crop 104 00:07:54,395 --> 00:07:56,458 up. Don't worry about it, it'll usually come 105 00:07:56,458 --> 00:07:58,852 up in some kinds of graphics programs, and you won't be able to see the 106 00:07:58,852 --> 00:08:01,735 difference between the two numbers anyways. 107 00:08:01,735 --> 00:08:07,980 Okay, now you've seen how to write expressions that operate on numbers. 108 00:08:07,980 --> 00:08:10,146 At this point, you should have a pretty good sense of how to write such 109 00:08:10,146 --> 00:08:12,388 expressions, and an intuitive understanding of what they're going to 110 00:08:12,388 --> 00:08:16,648 evaluate to. In the next video, we're going to look at 111 00:08:16,648 --> 00:08:19,970 the more precise roles that Racket uses for evaluating them. 112 00:08:21,660 --> 00:08:25,080 You may wonder at this point, are there more primitives that operate on numbers. 113 00:08:25,080 --> 00:08:28,970 And the answer is that there are lots. Lots and lots and lots. 114 00:08:28,970 --> 00:08:31,290 Far more than you can hope to learn at this point. 115 00:08:32,400 --> 00:08:35,390 But if you want to discover some of those primitives, I suggest you jump ahead to 116 00:08:35,390 --> 00:08:39,080 the video on Discovering Primitives, that comes later this week. 117 00:08:40,090 --> 00:08:42,842 Of course, it's also fine to just stick with the primitives we learn now, plus 118 00:08:42,842 --> 00:08:45,590 and times and minus and square and square root.