We've been learning about different kinds of values, numbers, strings, and images,
and how to write expressions that operate on those values.
In this video, we're going to look at something quite different.
We're going to look at how to write constant definitions.
To give names to values so that we can use those values in different parts of
the program. Constant definitions are more than just a
convenience. We're going to see later in the course
that they help us make programs that are easy for other people to read and easy
for other people to change. And that those two properties,
readability and changeability, are two of the most important properties the program
can have. But for now, we're just going to focus on
the mechanism of constant definitions. So that you can learn how to write a
constant definition to give names to a value that you use in other parts of the
program. So.
let's imagine that we're working on a program in which there's a simple
interactive game running in a window. One thing we might want to do is give
names to the width and height of the window.
And the way we do that is we write open param define And then sone name, which we
often put the names of constants in uppercase, like this.
And we might say that the window is 400 pixels wide, and maybe the window is 600
high. And that's to name constants.
If we run this program just like this We don't get any values out, because
defining a constant doesn't produce a value.
But now what I can do is I can write an expression, like this, and what happens
is when Dr. Racket evaluates this When it comes to
evaluating the name of a defined constant it's just going to use the constant's
value there. So this will become 400.
So the evaluation steps here would be times width, which will become 400.
That's the first evaluation [/g] step. The next evaluation step height.
We'll valuate to 600 At this point, all the arguments to times are values, so now
we have 240,000. So the way to form a constant definition
is to have an open parenthesis or define a space and then a name.
And names typically include letters of the alphabet in upper or lower case they
can also include numerical digits and in the beginning student language they can
include lots of other characters like exclimation mark and question mark and
equals and things like that. Some languages are more restrictive about
that, but the beginning of student language is fairly, fairly generous about
that. You can use parentheses or quotes of any
kind now. The rules for evaluating a constant
definition are when Racket encounters the constant definition It evaluates the
expression, and uses the resulting value as the value of the name constant going
forward. When evaluation encounters a name
constant, the name constant simply evaluates to The value that the define
associated with it. Let's look at another example.
We get rid of all this now. Suppose in this same program, in my game
program. What we have is an image of a cat going
back and forth across the screen. Well, I might want to give a name to that
image that I can use in lots of places. So there's the cat.
And what I'm going to do here is go to the second edition of the How to Design
Programs book, which we use as the basis for the material in this Of course and
they happen to have a little cat here that we're going to use.
So I'm just going to go copy this image. And I'll come back over here and I'll
just paste it right in here. And what's happening in Racket is
remember that images is values And all values are expressions.
So I can stick that image right there in the program, as an expression.
[SOUND] Most languages won't let you do this.
But it's quite convenient to be able to do it in, in DSL, so we're going to take
advantage of that. So now I've got a cat.
And I can write some expressions. So here rotate is a primitive, and this
says rotate the cat by ten degrees to the left.
And here's another expression that would rotate.
The cat, rotate the cat by minus ten degrees, is what that one does.
This will rotate the cat by ten degrees. If we run this now, we see, first
expression produces the cat rotated this way, the second expression produces the
cat Rotated that way, and, we might even want to give names to these, cause
remember I said that we can have an expression here after the constant name.
So now I've given names. [NOISE] To those two rotated cats.
Oops. I can't use those names.
I've got to use different names. You can only define a given constant name
one time. That's why it's called a constant name.
It can't change. So I'll call those the RCAT and the LCAT.
And there they are. So here you see that what Racket is
letting us do is. They define in the name and then in
expression. That expression gets evaluated, and for
evermore that value will be associated with the constant name.
So we can use it in other places in the program.
I have an exercise I'd like to ask you to do now.
This will help you test your understanding of the material in this
video. So, now you've seen how we can define
constants in beginning student language. As I said at the beginning of this video,
we're going to see more and more how important this is.
Really this notion of defining name constants is going to have a big effect
on making our programs comprehensible to other programmers and easy to change in
the future. Let me take a minute now to talk about
the pace of the course. If these videos have been going too
slowly for you. If maybe you've already programmed, then
I just need to ask you to be patient. I promise you we're going to get there.
If on the other hand maybe you haven't programmed before.
Some of you might find these videos going a bit too quickly.
One thing we've learned at UBC is that for some students it takes a little bit
longer to get the hang of this. If that's the case for you, if you feel
like it's going a bit too quickly, let me make three suggestions.
First, go back through the exercises that you had a hard time with and work them
again. Second, you can watch that part of the
video again. But most importantly let me suggest you
go to the week one page. Look at the instructions for how to get
help, and post a message asking for help. One of the things we've learned in the
thousands of students that we've taught this material to is it really helps a lot
to ask other people and discuss the issues that your having a hard time with.
That's going to be a big part of this course going forward.