So far you've seen how to write expressions that operate on numbers, and
you've learned the primitive call rule for evaluating those expressions.
But numbers aren't the only primitive kind of data in Racket.
In this video we're going to learn about strings, which are used to represent
words and names, and images which let us start to build up pictures.
We won't have to learn any new evaluation rules though, because calls to string and
image primitives, work just like calls to number primitives.
So even though this video is a little bit long, I think you'll find it moves long
quite quickly. First we'll look at string primitives,
then we'll look at some image primitives. There's one tricky part having to do with
the way we number the characters in a string, but I plan to spend a couple
minutes on that, just to be sure to show you how it works.
In the beginning student language strings looks like this.
There's a double quote, and then some characters, like apple, and then another
double quote. And Racket highlights it, the string
between the quotes with green, for us, to help us see it.
So that's a string. Here's another string, Ada, someone's
name. And strings are values.
So if we run the program with these two strings in it, the value of the strings
is the strings themselves. So apple produces apple, and Ada produces
Ada. Now what are some things we can do with
strings? Well, one thing we can do with strings,
is put them together. So let's say we have two strings.
Ada which is someone's first name and Lovelace which is someone's last name.
We could put them together like that. And if we run that, string-append is an
operation kind of like plus, but for strings it glues the two strings together
like that. And probably if its somebody's name, we
might want to put another space in there. And one way to do that, would be to just
add a third argument to string-append that adds the extra space in there.
And there we go, and there we get Ada Lovelace.
You might want to look up, she's very famous for Computer Scientist.
In the 1840s, the 1840s, she wrote the first computer program ever written.
It was written for a machine which at that time only existed on paper.
The machine itself didn't run until quite recently, but she wrote the program in
the 1840s. So that's one thing we can do with
strings. We've got strings, we can put strings
together like that. Let me just point out one more wrinkle
about strings. This is a string, that is a string that
happens to have the characters 1 2 and 3 in it.
And this is a number, 123, and they're not the same.
So in particular, you could take the number and, add 1 to it.
We run that program. We get 124 as the result of that
expression. But you can't take the string, and add 1
to it. If you try to do that, you will get this
error message. Plus expects a number as its 2nd
argument, you gave it a string. And Racket nicely highlights, for us
where the error is. And you'll probably make this mistake.
Everybody makes it, makes it at first when they're learning the difference
between strings and numbers. And you just look at that and you say, oh
I meant this to be a number. And you just get rid of those string
quotes. And now this program does run and, and
both of those are working. We get 124 twice.
[NOISE] Okay, let me show you two more primitives on strings very quickly.
What I'll do is, I'll delete that stuff, and I'll comment this stuff out.
One operation we can do on strings, is to take string-length, for example of apple.
And string-length is a primitive, which tells us how long the string is.
and that string is, let's see a,p,p,l,e. Five characters long, sure enough.
So that's one thing we can do with strings.
There's another operation we have called substring, and substring is going to let
us take out parts of the string. So, if for example, we have a string like
Caribou. Substring let's us take out parts of the
string. But let me just give you an example here.
If I say Caribou 2 and 4. That's going to mean, take out all the
characters from 2 to 4, give us just those characters.
Now that's ri, and the question is why is that ri?
Well there's a funny little trick here that computer scientists have played on
the world, which is to use something called 0 based indexing.
In order to understand 0 based indexing, the way I always do it, is I make myself
a string that looks like this. I make a little string that's 012345678.
That's probably long enough for now. 'Kay?
And what 0 based indexing means, is it means that for substring, if we operate
in this string here 012345678, and we go from 2 to 4.
It means start at character number 2 in 0 based indexing, and go right up to, but
not including, character number 4 in 0 based indexing.
So again, if you take this, what I'll do is I'll take this string here, and I'll
line it up perfectly with Caribou, and now you can see why we get ri.
Because its r, it starts are 2, and we end right before 4.
So that's the trick of 0 based indexing. And you know, a lot of people make
mistakes about this, they get what are called off by one errors, because they
confuse the 0 based indexing. Again, don't worry about it if that
happens. One of the great things about computer
programming, is these machines are not fragile.
If you make a mistake, you get an error message, not quite the right thing
happens, you just fix it. I'm not saying we shouldn't worry about
errors in our programs. The design method we're going to learn,
is going to help us prevent and find such errors.
All I'm saying is, if you get an error while you're working, don't worry about
it. Look at the error message and fix the
problem. So just so you see one other example of
substring. Let's say, if I say substring of Caribou
and I want just the first 3 characters, I would say 0 and I'll stop at 3.
[NOISE] That gives me the C a r. So I've got an exercise, I'd would like
you to do now. Please do the exercise, and we'll go on
with images after this. Okay, that's some basics about strings.
What I'd like to do now, is go look at some basics about images.
So what I'll so is I'll make a new tab. And DrRacket has lots of different kinds
of image functions. In order to tell it that we want to use
the ones for this course, we're going to type at the top of this file, and any
file in this course that uses image functions, require 2htdp/image.
And what this is telling DrRacket, is it means we want to use the image functions
that come from the 2nd edition of the How to Design Programs book.
So it's a little bit of a mouthful, but you can just type that at the top of any
file in which we're using image functions.
Now there's lots and lots of image primitives.
Some of them make images. For example, the circle primitive.
The first argument to circle, is the radius in screen coordinates or pixels.
The second argument to circles says whether the circle should be solid or an
outline. And the third argument is a color.
So that expression there, produces that red circle.
So we call the primitive circle, and we get a red circle.
And there's lots of similar shapes, rectangle, rectangle takes the width and
a height, and it could take outline or solid.
And it has different resu, we can try some of our other colors.
There we go we got a rectangle. And there's just a whole bunch of them,
I'm not going to go through all of them right now.
Another one that's quite useful though, is called text.
Text a string, for example hello, in a font size like 24.
And another color like orange. And it produces, this is now an image of
the string hello in font size 24, color orange.
So this is an image now. It's not a string.
So that's a bunch of useful primitives for making images.
Let me talk about some things we can do with the images, once we have them made.
One useful primitive is above. So if I say, above we'll make circle of
size 10, that's solid and red. And I'll make you another couple of
circles, what I'll do is I'll do this easily by Cutting and Pasting, and then
I'll just change the sizes. Let's say we'll make this one 20, and
we'll make this one 30, and to make it pretty we can make, change the colors
too. There we go.
So now what happens when I run that, is, I get this stack of the images.
Above takes all its, all of the images that it receives as arguments, and it
stacks them one on top of the other. So above is kind of, sort of a version
string-append for images. But since images can be arrayed in lots
of different ways, there's lots of other functions.
So in addition to above there's, for example, beside.
And if I run that, I get that shape. In addition to beside, there's a thing
called overlay. Overlay.
And if I run that, the overlay stacks them on top of each other.
There's lots and lots of primitives that operate on images.
And as you do the homework exercises this week, you'll get a chance to look them up
and play with them. But, for example, there's functions that
make ellipses and stars and triangles and things like that, and you can line images
up, and put them next to each other in different ways.
But these are a good set of basic functions right here.
Circle, rectangle, text, overlay, above, and beside.
So I've got another little exercise you can do here, to test your understanding
about, primitives that operate on images.