[MUSIC] This segment is the beginning of
the first of a couple of sections and we’ll use the Racket language,
so we need to start by getting used to a new programming language and we’ll do
that here. so we’re going to use the Racket language
instead of ML. We’re going to recommend the DrRacket
programming environment instead of another editor, so we won’t use Emacs for
this part of the course, and we have installation and usage
instructions on the course website. I think you will find installation here
very simple and straightforward, so just do it when you have a few minutes.
Like ML, Racket is a mostly functional language, so a lot of what we’ve learned
in ML, we just get to see in a new setting.
We’ll still have anonymous functions, we’ll have first class function closures,
everything is an expressions, so we need things like return statements and so on.
We’re not going to use Racket with a form of case expression, it does have support
for some of that, but were going to access our one of types
in a different way and that will be fine for our purposes.
But there’s two key differences from ML that make me want to use Racket for this
portion of the course. The first, is that it does not rely so
much on a static type system, it accepts many more programs and that
just delays what in ML would be a type error until something occurs at run-time.
So if you want to try to add a number and a string it will be just fine until you
actually get to that expression and then you will get a run-time error.
The other thing, as you’ll see in future segments when we start writing some real
code, is that Racket has a very minimalist syntax.
It uses parentheses a lot to group things rather than having strange syntax rules,
like most other programming languages. and it has many advanced features.
we won’t have time for most of them, but I want to at least have some optional
discussion of the module system if we have time,
I do want to discuss macros and so on. Now overall, because of the similarities
with ML, the next homework assignment is not just going to be a get used to Racket
assignment. The first problem or two will be, but
after that, I want to talk about some new concepts, some new things that we could
have done in ML, but worked out a little more cleanly in Racket.
So we’ll have some segments to get used to the Racket, the language, but then,
we’re going to move on, because the course is not just about trying out the
same thing in different languages, it’s about learning new concepts.
I should mention that there’s a related programming language called Scheme.
Racket essentially evolved out of Scheme and was evolving for a while, and about
2010, the designers of Racket decided to stop using the Scheme name.
so that they could be more different without feeling bad about it, if you
will. I might occasionally slip up and say
Scheme, because I’m still used to calling languages like this, Scheme.
I’ll try not to but just don’t be confused if I do so.
And, in case you have programmed in Scheme, and it was a very popular
language, particularly in introductory programming languages, as well as for
real use, Racket has made some non-compatible changes.
The most, the ones you’re most likely to notice are related to how lists are used,
and in particular, that like ML list elements are not mutable.
Okay? So, if you’ve seen Scheme before, you do need to get used to that.
If not, we’ll just introduce Racket as its own wonderful modern programming
language. I should mention that, as a modern
language, it’s been used to build some real systems and continues to do so to be
used in that way. it’s also used a lot in education, which
is probably what it’s best known for. the language does continue to evolve, so
it can be a bit of a moving target. I’m trying to keep everything up to date.
It doesn’t move that quickly and you can always consult the online documentation
in particular, the Racket guide is a free user’s guide that covers the important
concepts of the language and has much more than what we need in this course,
but it’s easy to look up things as you need them.
So, to get started, I’m going to show you DrRacket here in just a second.
It’s going to have what it calls a definitions window and an interactions
window, this will feel very familiar to us.
in Emacs, we had the buffer where we wrote the code, and the REPL where we ran
the code. DrRacket is going to work the same way.
I think you’ll find it more user-friendly than how we used ML.
and, you know, it will be fairly easy to learn on your own,
ask questions if you have them on the discussion forum, and the lecture demos,
of course, will show me using the tool. And as I mentioned, there’s wonderful
documentation, the general Racket website is where you
download DrRacket and there’s the Racket Guide, there’s many tutorials there’s no
shortage of information. Okay. So why don’t I flip over here now?
So here is DrRacket and this is just the current buffer I’m showing you is, is
just a buffer where you can write code and then, when I want to run it, I can
just click this Run button here in the upper right, and now, I get this split
where I have a REPL below and the code up above.
You can flip between these, the Menu options have options to show, show just
one or the other. I think control E-cycles between showing
just the definitions and the definitions and the REPL.
So, I’m just hitting Contol-E to switch back and forth here,
and this is pretty much what DrRacket will look like for you, except I’ve
increased the font size and changed the font, so that it looks a little better in
the recordings. So now let’s look at the actual code.
this brownish text that you see are comments.
Comments in Racket start with a semicolon and go until the end of the line.
There is support for multi-line comments. I won’t tend to use them much.
this is fairly conventional to just have each line of a comments start with
semicolon and you can have comments on the same line as other code, you just
have to have it to the right. So this here down at the bottom, this is
another comment. A couple bookkeeping things we’re always
going to do in our files. The first is to always have the first
non-comment line in your file be exactly this.
I’ll type it again for you, although, you should only have it once, #lang racket.
Okay? that tells DrRacket that this is Racket code in this file.
DrRacket actually supports defining your own languages, running code in lots of
different languages, so we have to say which language our code is and we say it
with this first line. This second line provided all-defined-out
is a bit of a workaround to keep things simple for us.
By default in Racket, it has a module system, just like we studied in ML, but
in Racket’s module system, each file is a module, and by default, everything in it
is private and you have to say what you want to make available to other files.
Using our methodology in this course of putting our tests in a second file,
that’s a bit cumbersome, so this one line which you can just copy or we’ll give it
to you for the homework assignments, says change the default,
make everything public, and that makes your code much easier to test.
So with those two things out of the way you then, in the rest of your file just
define a bunch of definitions, variables, other things. And for this lecture,
I’ve just done one, we’ll do a bunch more in the bunch more
in the next segment, and I’ve defined the variable s to be the string constant,
hello. So when you define a variable, it’s open
parentheses, keyword define, name of your variable, string hello, close
parenthesis. So, this is like a val binding in ML.
This would be like val s=hello. And if I go to run this, when I click
this on the REPL, it did run all the codes.
So it did create that definition for s, unlike in ML, it doesn’t tell us any
information. it just you know, it would give us an
error if something didn’t work, but since everything did work, it just
gives us our prompt. And I could say here, I could add 2 and
2, and I would get back 4.
That’s how you do addition in Racket, parenthesis, plus, the arguments, and
another parenthesis. I do have s, right? I get the string
hello right there. If I use a different variable, like t, I
get an error message that I have a reference to an unbound identifier.
So that’s just seeing DrRacket, we’ll continue to use it in the next few
segments as we introduce the Racket language, quickly covering a lot of the
same ideas we already saw in ML. And then after a few segments of that,
we’ll move onto new concepts and new material.