1 00:00:06,630 --> 00:00:09,330 So far you've seen how to write expressions that operate on numbers, and 2 00:00:09,330 --> 00:00:13,190 you've learned the primitive call rule for evaluating those expressions. 3 00:00:14,540 --> 00:00:17,900 But numbers aren't the only primitive kind of data in Racket. 4 00:00:17,900 --> 00:00:21,356 In this video we're going to learn about strings, which are used to represent 5 00:00:21,356 --> 00:00:25,810 words and names, and images which let us start to build up pictures. 6 00:00:27,350 --> 00:00:31,421 We won't have to learn any new evaluation rules though, because calls to string and 7 00:00:31,421 --> 00:00:35,894 image primitives, work just like calls to number primitives. 8 00:00:35,894 --> 00:00:39,664 So even though this video is a little bit long, I think you'll find it moves long 9 00:00:39,664 --> 00:00:43,692 quite quickly. First we'll look at string primitives, 10 00:00:43,692 --> 00:00:48,384 then we'll look at some image primitives. There's one tricky part having to do with 11 00:00:48,384 --> 00:00:51,098 the way we number the characters in a string, but I plan to spend a couple 12 00:00:51,098 --> 00:00:55,040 minutes on that, just to be sure to show you how it works. 13 00:00:57,810 --> 00:01:02,071 In the beginning student language strings looks like this. 14 00:01:02,071 --> 00:01:06,889 There's a double quote, and then some characters, like apple, and then another 15 00:01:06,889 --> 00:01:10,939 double quote. And Racket highlights it, the string 16 00:01:10,939 --> 00:01:15,307 between the quotes with green, for us, to help us see it. 17 00:01:15,307 --> 00:01:20,157 So that's a string. Here's another string, Ada, someone's 18 00:01:20,157 --> 00:01:23,386 name. And strings are values. 19 00:01:23,386 --> 00:01:29,182 So if we run the program with these two strings in it, the value of the strings 20 00:01:29,182 --> 00:01:34,930 is the strings themselves. So apple produces apple, and Ada produces 21 00:01:34,930 --> 00:01:38,923 Ada. Now what are some things we can do with 22 00:01:38,923 --> 00:01:43,772 strings? Well, one thing we can do with strings, 23 00:01:43,772 --> 00:01:50,360 is put them together. So let's say we have two strings. 24 00:01:50,360 --> 00:01:55,421 Ada which is someone's first name and Lovelace which is someone's last name. 25 00:01:55,421 --> 00:01:59,485 We could put them together like that. And if we run that, string-append is an 26 00:01:59,485 --> 00:02:02,685 operation kind of like plus, but for strings it glues the two strings together 27 00:02:02,685 --> 00:02:07,825 like that. And probably if its somebody's name, we 28 00:02:07,825 --> 00:02:14,599 might want to put another space in there. And one way to do that, would be to just 29 00:02:14,599 --> 00:02:19,420 add a third argument to string-append that adds the extra space in there. 30 00:02:19,420 --> 00:02:21,220 And there we go, and there we get Ada Lovelace. 31 00:02:21,220 --> 00:02:25,663 You might want to look up, she's very famous for Computer Scientist. 32 00:02:25,663 --> 00:02:33,170 In the 1840s, the 1840s, she wrote the first computer program ever written. 33 00:02:33,170 --> 00:02:37,022 It was written for a machine which at that time only existed on paper. 34 00:02:37,022 --> 00:02:42,622 The machine itself didn't run until quite recently, but she wrote the program in 35 00:02:42,622 --> 00:02:45,584 the 1840s. So that's one thing we can do with 36 00:02:45,584 --> 00:02:48,135 strings. We've got strings, we can put strings 37 00:02:48,135 --> 00:02:50,996 together like that. Let me just point out one more wrinkle 38 00:02:50,996 --> 00:02:54,146 about strings. This is a string, that is a string that 39 00:02:54,146 --> 00:02:58,500 happens to have the characters 1 2 and 3 in it. 40 00:02:58,500 --> 00:03:02,890 And this is a number, 123, and they're not the same. 41 00:03:02,890 --> 00:03:10,780 So in particular, you could take the number and, add 1 to it. 42 00:03:10,780 --> 00:03:13,535 We run that program. We get 124 as the result of that 43 00:03:13,535 --> 00:03:19,460 expression. But you can't take the string, and add 1 44 00:03:19,460 --> 00:03:23,208 to it. If you try to do that, you will get this 45 00:03:23,208 --> 00:03:26,133 error message. Plus expects a number as its 2nd 46 00:03:26,133 --> 00:03:31,096 argument, you gave it a string. And Racket nicely highlights, for us 47 00:03:31,096 --> 00:03:35,140 where the error is. And you'll probably make this mistake. 48 00:03:35,140 --> 00:03:37,471 Everybody makes it, makes it at first when they're learning the difference 49 00:03:37,471 --> 00:03:40,684 between strings and numbers. And you just look at that and you say, oh 50 00:03:40,684 --> 00:03:45,019 I meant this to be a number. And you just get rid of those string 51 00:03:45,019 --> 00:03:47,302 quotes. And now this program does run and, and 52 00:03:47,302 --> 00:03:50,108 both of those are working. We get 124 twice. 53 00:03:50,108 --> 00:03:57,300 [NOISE] Okay, let me show you two more primitives on strings very quickly. 54 00:03:57,300 --> 00:04:01,650 What I'll do is, I'll delete that stuff, and I'll comment this stuff out. 55 00:04:01,650 --> 00:04:11,500 One operation we can do on strings, is to take string-length, for example of apple. 56 00:04:11,500 --> 00:04:16,517 And string-length is a primitive, which tells us how long the string is. 57 00:04:16,517 --> 00:04:20,982 and that string is, let's see a,p,p,l,e. Five characters long, sure enough. 58 00:04:20,982 --> 00:04:25,380 So that's one thing we can do with strings. 59 00:04:25,380 --> 00:04:29,534 There's another operation we have called substring, and substring is going to let 60 00:04:29,534 --> 00:04:35,148 us take out parts of the string. So, if for example, we have a string like 61 00:04:35,148 --> 00:04:39,269 Caribou. Substring let's us take out parts of the 62 00:04:39,269 --> 00:04:42,800 string. But let me just give you an example here. 63 00:04:42,800 --> 00:04:47,390 If I say Caribou 2 and 4. That's going to mean, take out all the 64 00:04:47,390 --> 00:04:54,128 characters from 2 to 4, give us just those characters. 65 00:04:54,128 --> 00:05:00,560 Now that's ri, and the question is why is that ri? 66 00:05:00,560 --> 00:05:03,926 Well there's a funny little trick here that computer scientists have played on 67 00:05:03,926 --> 00:05:07,768 the world, which is to use something called 0 based indexing. 68 00:05:07,768 --> 00:05:12,253 In order to understand 0 based indexing, the way I always do it, is I make myself 69 00:05:12,253 --> 00:05:20,190 a string that looks like this. I make a little string that's 012345678. 70 00:05:20,190 --> 00:05:22,159 That's probably long enough for now. 'Kay? 71 00:05:22,159 --> 00:05:29,519 And what 0 based indexing means, is it means that for substring, if we operate 72 00:05:29,519 --> 00:05:37,359 in this string here 012345678, and we go from 2 to 4. 73 00:05:37,359 --> 00:05:44,834 It means start at character number 2 in 0 based indexing, and go right up to, but 74 00:05:44,834 --> 00:05:53,020 not including, character number 4 in 0 based indexing. 75 00:05:53,020 --> 00:05:58,972 So again, if you take this, what I'll do is I'll take this string here, and I'll 76 00:05:58,972 --> 00:06:07,063 line it up perfectly with Caribou, and now you can see why we get ri. 77 00:06:07,063 --> 00:06:11,161 Because its r, it starts are 2, and we end right before 4. 78 00:06:11,161 --> 00:06:15,924 So that's the trick of 0 based indexing. And you know, a lot of people make 79 00:06:15,924 --> 00:06:20,404 mistakes about this, they get what are called off by one errors, because they 80 00:06:20,404 --> 00:06:25,575 confuse the 0 based indexing. Again, don't worry about it if that 81 00:06:25,575 --> 00:06:27,207 happens. One of the great things about computer 82 00:06:27,207 --> 00:06:29,075 programming, is these machines are not fragile. 83 00:06:29,075 --> 00:06:32,975 If you make a mistake, you get an error message, not quite the right thing 84 00:06:32,975 --> 00:06:38,933 happens, you just fix it. I'm not saying we shouldn't worry about 85 00:06:38,933 --> 00:06:41,707 errors in our programs. The design method we're going to learn, 86 00:06:41,707 --> 00:06:44,460 is going to help us prevent and find such errors. 87 00:06:44,460 --> 00:06:48,556 All I'm saying is, if you get an error while you're working, don't worry about 88 00:06:48,556 --> 00:06:51,670 it. Look at the error message and fix the 89 00:06:51,670 --> 00:06:55,964 problem. So just so you see one other example of 90 00:06:55,964 --> 00:07:01,735 substring. Let's say, if I say substring of Caribou 91 00:07:01,735 --> 00:07:11,125 and I want just the first 3 characters, I would say 0 and I'll stop at 3. 92 00:07:11,125 --> 00:07:19,201 [NOISE] That gives me the C a r. So I've got an exercise, I'd would like 93 00:07:19,201 --> 00:07:23,874 you to do now. Please do the exercise, and we'll go on 94 00:07:23,874 --> 00:07:36,604 with images after this. Okay, that's some basics about strings. 95 00:07:36,604 --> 00:07:40,420 What I'd like to do now, is go look at some basics about images. 96 00:07:40,420 --> 00:07:45,491 So what I'll so is I'll make a new tab. And DrRacket has lots of different kinds 97 00:07:45,491 --> 00:07:49,763 of image functions. In order to tell it that we want to use 98 00:07:49,763 --> 00:07:54,661 the ones for this course, we're going to type at the top of this file, and any 99 00:07:54,661 --> 00:08:02,234 file in this course that uses image functions, require 2htdp/image. 100 00:08:02,234 --> 00:08:06,392 And what this is telling DrRacket, is it means we want to use the image functions 101 00:08:06,392 --> 00:08:11,600 that come from the 2nd edition of the How to Design Programs book. 102 00:08:11,600 --> 00:08:14,514 So it's a little bit of a mouthful, but you can just type that at the top of any 103 00:08:14,514 --> 00:08:18,410 file in which we're using image functions. 104 00:08:18,410 --> 00:08:20,810 Now there's lots and lots of image primitives. 105 00:08:20,810 --> 00:08:24,245 Some of them make images. For example, the circle primitive. 106 00:08:24,245 --> 00:08:28,910 The first argument to circle, is the radius in screen coordinates or pixels. 107 00:08:28,910 --> 00:08:33,383 The second argument to circles says whether the circle should be solid or an 108 00:08:33,383 --> 00:08:36,750 outline. And the third argument is a color. 109 00:08:36,750 --> 00:08:41,639 So that expression there, produces that red circle. 110 00:08:41,639 --> 00:08:46,963 So we call the primitive circle, and we get a red circle. 111 00:08:46,963 --> 00:08:53,138 And there's lots of similar shapes, rectangle, rectangle takes the width and 112 00:08:53,138 --> 00:08:58,685 a height, and it could take outline or solid. 113 00:08:58,685 --> 00:09:02,178 And it has different resu, we can try some of our other colors. 114 00:09:02,178 --> 00:09:05,624 There we go we got a rectangle. And there's just a whole bunch of them, 115 00:09:05,624 --> 00:09:08,855 I'm not going to go through all of them right now. 116 00:09:08,855 --> 00:09:14,497 Another one that's quite useful though, is called text. 117 00:09:14,497 --> 00:09:20,710 Text a string, for example hello, in a font size like 24. 118 00:09:20,710 --> 00:09:27,928 And another color like orange. And it produces, this is now an image of 119 00:09:27,928 --> 00:09:36,010 the string hello in font size 24, color orange. 120 00:09:36,010 --> 00:09:38,758 So this is an image now. It's not a string. 121 00:09:38,758 --> 00:09:42,837 So that's a bunch of useful primitives for making images. 122 00:09:42,837 --> 00:09:48,490 Let me talk about some things we can do with the images, once we have them made. 123 00:09:48,490 --> 00:09:56,882 One useful primitive is above. So if I say, above we'll make circle of 124 00:09:56,882 --> 00:10:02,180 size 10, that's solid and red. And I'll make you another couple of 125 00:10:02,180 --> 00:10:07,535 circles, what I'll do is I'll do this easily by Cutting and Pasting, and then 126 00:10:07,535 --> 00:10:14,246 I'll just change the sizes. Let's say we'll make this one 20, and 127 00:10:14,246 --> 00:10:19,370 we'll make this one 30, and to make it pretty we can make, change the colors 128 00:10:19,370 --> 00:10:22,875 too. There we go. 129 00:10:22,875 --> 00:10:30,150 So now what happens when I run that, is, I get this stack of the images. 130 00:10:30,150 --> 00:10:34,304 Above takes all its, all of the images that it receives as arguments, and it 131 00:10:34,304 --> 00:10:40,995 stacks them one on top of the other. So above is kind of, sort of a version 132 00:10:40,995 --> 00:10:45,080 string-append for images. But since images can be arrayed in lots 133 00:10:45,080 --> 00:10:48,070 of different ways, there's lots of other functions. 134 00:10:48,070 --> 00:10:50,535 So in addition to above there's, for example, beside. 135 00:10:50,535 --> 00:10:58,534 And if I run that, I get that shape. In addition to beside, there's a thing 136 00:10:58,534 --> 00:11:01,960 called overlay. Overlay. 137 00:11:01,960 --> 00:11:08,539 And if I run that, the overlay stacks them on top of each other. 138 00:11:10,360 --> 00:11:13,270 There's lots and lots of primitives that operate on images. 139 00:11:13,270 --> 00:11:16,486 And as you do the homework exercises this week, you'll get a chance to look them up 140 00:11:16,486 --> 00:11:20,705 and play with them. But, for example, there's functions that 141 00:11:20,705 --> 00:11:24,500 make ellipses and stars and triangles and things like that, and you can line images 142 00:11:24,500 --> 00:11:28,682 up, and put them next to each other in different ways. 143 00:11:28,682 --> 00:11:31,340 But these are a good set of basic functions right here. 144 00:11:31,340 --> 00:11:34,910 Circle, rectangle, text, overlay, above, and beside. 145 00:11:34,910 --> 00:11:39,596 So I've got another little exercise you can do here, to test your understanding 146 00:11:39,596 --> 00:11:43,569 about, primitives that operate on images.