So as I said in the introduction, we're going to design some great programs in
this course. Some games, some animations, some
information visualization, some web programs.
Really great stuff. But the thing is, you gotta walk before
you can run. And in the same way, we've gotta learn
some basic building blocks, before we can build these great programs.
So that's what we're going to do for the next few videos, is learn some basic
building blocks out of which we're going to build bigger programs.
We're going to try to do it quickly, because it isn't that exciting.
But we're going to try not to do it too quickly, because we want everybody to be
able to stay with us. So, if maybe you've programmed before,
and this video seems a little slow, then feel free to speed it up.
But most of the people who take this class haven't programmed before, so we're
going to try to go a good speed for that. And i, and if it is a little too fast,
then you can replay the part of the video that you need to replay.
As we go through it, I'd encourage you to have DrRacket open, and follow along with
the examples that I'm going to do. So here we go.
When we start Racket for the first time, we have to tell it what language we're
going to use. Your Racket may have started up already
saying beginning student language down in this lower left corner.
If it did, then you're all set, you don't need to do anything.
But if it didn't, then go to the Language menu.
Say Choose Language. Make sure the How to Design Programs part
is opened up, and choose Beginning Student.
The way you'll get to this menu will be slightly different in windows of course,
but your going to want to go to the Language menu to do that.
So go to the Language menu, and select Beginning Student Language.
And then you'll be all set to go. Now that, that's done, we can get
started. The top part of Racket here is called the
definitions area, and the bottom part is called the interaction area.
We're going to start by working up in the definitions area.
And we're going to start by writing a simple arithmetic expression.
I'll just put plus 3 4. This is how we're going to say to add 3
to 4, in the beginning student language. And if I ask DrRacket to run that short
program, it will, and down here it tells me that the result of that program is 7.
Which isn't so surprising, adding 3 to 4 should produce 7.
This is what's called an expression, and in the bottom window we have what's
called a value. And the way Racket is working, is we give
it expressions, and it evaluates the expression to give it the value.
Expressions can be more complicated, for example, we can say plus 3 times 2 3.
And we can run that program. And most surprised, it produces 9.
We can make expressions that are even more complicated than that, or use other
primitive operators. Here we'll just divide 12 by times 2, 3.
And unsurprisingly, that will get us 2. So what we've seen so far, is that the
rule for the way we form an expression is open parenthesis, the name of a primitive
operator like plus, or times, or slash. And then any number of other expressions,
followed by a closed parentheses. And there's another rule that says
expressions can be actual values, so numbers themselves can be expressions.
Let me show you another thing we could do.
We could take all of this. And if we say to Racket, comment that out
with semicolon. And then we'll put a semi colon in front
of each of those lines, and what that tells Racket is, for now it should ignore
everything on a line after the first semi colon.
So if I would run this program now, as far as Racket's concerned there's no
expressions in there at all, and so there's no values that come out.
Let me tell you about two more primitive operations or numbers, and then I'll ask
you to do an exercise. The first one is sqr, we call it square.
And if I take square of 3, and I'll show you another one here at the same time,
the square root of 16, and I'll run these two.
And what you're seeing is that square, squares the number, and square root
produces the square root of the number. Okay, we've seen how to form expressions,
and we've seen a number of primitives that operate on numbers, primitives like
plus and times and divide and minus, and square and square root.
What I like to do now, is give you an exercise that you can use to help
reinforce what we've learned so far. Well the exercises is like this
throughout the videos in this course. I'd like to encourage you to do them,
just to test your understanding of what's come so far.
So here's the exercise. I've got it in a separate file, and let
me just explain this big box. Racket has a thing called a comment box,
which you can actually add to a file yourself, under the Insert menu.
And the way to think of a comment box, is, see this semicolon here?
It's basically saying everything in the box is a comment.
And what's also need about Racket is that you can put images in the middle of
files. That just an image that I Cut and Pasted.
And anyway, here's the exercise. Go ahead and work on it.
Write the expression. And when you're done, restart the video.
I hope you enjoyed doing that exercise. Let me just quickly show you how I think
about it. Given the formula, I know that I need to
square 3, and I need to square 4. And I need to add those together.
And I need to take the square root of that whole thing.
And if I run that, I get 5. Sure enough.
Let me say a word here about math. The Pythagorean theor, theorem here, is
pretty much the hardest math we're going to do in this whole course.
And that's important, because you need to know that to design a lot of programs,
you don't need to know a lot of math. You can be a very good program designer,
without knowing a lot of math. There are certain types of programs,
programs in graphics or vision or machine learning, where you do need to l, know a
lot of math. That's because you need to understand
that domain. We won't do a lot of math in this course,
though. The Pythagorean theorem here is about the
hardest we'll do. There is, however, one little detail that
I need to tell you. A little bit more math, and then we'll go
on. [SOUND] If I take the square root of 2,
and run that. Look at this funny thing Racket is
telling me that I see. Why is that?
Well you may know that the square root of 2 is a irrational number.
And what it means to be an irrational number, is that it takes infinite space
to write the number down. Well, computers are not infinite in size.
They're quite finite in size. So it has no way of representing a
number, that takes infinite space to write it down.
So what this means, this sharp sign I and a number means, it's an inexact number.
It's telling you that the number is pretty close to this number here, but not
exactly. And you may see numbers like that crop
up. Don't worry about it, it'll usually come
up in some kinds of graphics programs, and you won't be able to see the
difference between the two numbers anyways.
Okay, now you've seen how to write expressions that operate on numbers.
At this point, you should have a pretty good sense of how to write such
expressions, and an intuitive understanding of what they're going to
evaluate to. In the next video, we're going to look at
the more precise roles that Racket uses for evaluating them.
You may wonder at this point, are there more primitives that operate on numbers.
And the answer is that there are lots. Lots and lots and lots.
Far more than you can hope to learn at this point.
But if you want to discover some of those primitives, I suggest you jump ahead to
the video on Discovering Primitives, that comes later this week.
Of course, it's also fine to just stick with the primitives we learn now, plus
and times and minus and square and square root.