In the next few videos, we're going to look at a more complex form of data than
we've seen before. This data involves something called
reference, and it comes up when the information we're trying to represent
naturally has different related parts. Now that only happens in problems that
are more complex than the ones we've seen so far.
So, in order to motivate this, I have to be working on a more complex problem and
a problem that will take several videos to solve.
Now, of course when I do that, there'll be parts of this problem, a number of
parts of this problem that are quite familiar.
That only take things that we've seen before.
So, as always when that's the situation, that's a good place to stop the video and
work ahead. In fact, to really encourage that this
time, there will be pauses in the video, breaks in the video where I jump ahead.
I won't actually work through some of the stuff that you've seen now many times
before. That's a place for you to do that
yourself and then see where you are when the video restarts.
Okay, the problem is in tuition graph starter.
What's going on here is that we're trying to design a program that can produce bar
charts of different kinds of information. The way I've got the problem set up is
that it's supposed to produce a bar chart of tuition information for an arbitrary
number of schools. We're saying that Eva has to decide where
to go to University, and she wants to make a plot of the different tuition
costs for Universities. So, it's a three part problem.
First, we have to design a data definition to represent the information
about the tuition at the different Universities.
Then we have to design a function that's going to consume a list like that and
produce a bar chart like this one here. Finally, we have to design a function
that's going to consume a list of tuition information at the different
universities, and produce the school that has the lowest tuition.
What I'm going to do now is that even though this isn't a word program, I can
see that there's some constants here related to the drawing.
Things like the color of the bars and the size of the font.
And so, we'll do a little bit of constant analysis and defining some contants now.
So, why don't you stop the video and you do that too and when you're done restart
the video, and you can compare what you've done to what I've done.
Now I've completed a little bit of constant analysis and defining some
constants. And I should say first, that if you
didn't see right away that the defining constants was a good thing to do here,
that's completely fine. Sometimes you identify constants up
front, sometimes you realize later that you'll need them.
If you didn't see that right away, that's fine.
Let me show you what I've done. I'm in a different version of the file
now, tuition graph V1. I added require 2htdp/Image at the top
because I knew I was going to be doing some drawing.
I've made it part of the file for constants, and I said you know, this font
size here that we're going to be drawing the name of the schools in, that looks
like a good constant. By defining a constant for it will be
easier for me to change later. And the font color similarly I made that
black. And I also realized that there would have
to be kind of a scaling factor, because these tuitions are going to be numbers
like $20,000. And I'm pretty sure I don't want to make
the graph 20,000 pixels high. That's going to be kind of a very high
graph. It won't fit on the screen or the piece
of paper, or anything. So, I invented a concept that I'm calling
the Y scale, or I could've called it the Y scale factor or something.
But it's basically how much I'm going to divide the dollars by to convert it to
the height of the bar, and I just guessed one over 200.
Maybe that'll end up being right, maybe it won't.
We'll see later. The bars have to have a width, I just
guessed that 30 pixels will look good. And the bars have to have a color, I just
guessed that light blue would look good. Remember, the reason we give names to
these constants is to make it easy to get the program going quickly, then you can
adjust them to look a little bit better. So, that's the constants.
The next thing I have to do is the data definitions for representing the
information in this problem. Remember, the information that we have to
represent is for an arbitrary number of schools.
We need to know for each school, what it's name is and what it's tuition is.
Now, what makes this problem different is we're going to have two data definitions.
There's going to be one compound data definition called School, and that's
going to be used to represent information about an individual school.
And then there's going to be an arbitrary size data definition called List of
School, which will represent an arbitrary number of those school units.
And what's going to happen there is the second data definition, list of school,
is going to refer to the first data definition, school.
And that's going to carry through the entire problem.
So, there will be what's called a reference relationship in the type
comment. There will be what's called a natural
helper in the templates. And in the final function, there will be
a helper function call. We've seen this before, it's a theme of
this part of the course. The structure of the information
determines the structure of the data, determines the structure of the
templates, determines the structure of the functions.
So now, let's go look at how this plays out in the code.
I'm in tuition graph V2, and I've started the data definition, but I've saved the
most interesting part to do now. In terms of the compound information
about the school, that's a compound data definition, like the ones we've seen
before. I defined a structure called school with
two fields, name and tuition. And I said that a school is a make school
where name is a string and tuition is a natural.
And I'm just saying here that name is the school's name and tuition is the
international students tuition in US dollars.
Remember we like to put units for numbers in interpretations, and I just decided on
US dollars. You can use whatever currency you want.
And then, I just put some examples, I said school one is a school called school
one. And it has a tuition of $27797.
And school two is $23,300, and school three is $20,500.
The template for school is straight forward, it's compound of two fields.
Both fields are primitive types, string and natural.
So, the template just ends up looking like that.
Now, the list of school data definition starts out just like we've seen before.
I'll say that list of school is one of either empty for the base case, or cons
something list of school, for the self reference case.
But the question is what's the something? And for the something here, rather than
putting a primitive type, I've put school, because that's what list of
school really needs it be. It needs to be a list of representations
of schools. So, that in this list, I'll have an
arbitrary number of schools. And then for each school, I'll have its
name and its tuition. And so, this type comment has something
quite new. And it's really the new thing in this
whole problem. In addition to the self reference
relationship, it also has what we want to call a reference relationship.
It refers right here to a non-primitive type.
And the way we're going to draw that is with an arrow, like the arrow we used for
self reference, but we're going to label it with an R for reference.
That picture of the two type comments with the two arrows is going to define
the structure of the entire rest of this problem.
Let me show you how that starts happening in the template.
Define fn-for-los los template rules used.
And it starts out pretty normally, I'll go, I'll speed things up a bit here.
[BLANK_AUDIO]
Now here's where it gets a little bit different.
We've already seen in the previous examples, that we know that rest of los
is going to be a value of type list of school, and that's going to cause us to
use the self reference rule. I'm going to skip a line here.
That's going to cause us to use the self reference rule.
And the self reference rule makes us put a natural recursion right here.
But remember, this type comment has a second arrow /g.
We know that the type of first los is going to be school.
And school is a non-primitive type, that's why we have the arrow labelled r.
And that's going to cause us to use what's called the reference rule.
And the reference rule says that first los is school, it's a non-primitive type.
So, what we're going to do right here, we're going to put in a call to the
template function for the referred to type.
That picture is a little hard to understand like that.
So, let me just rearrange it a little bit.
And what I've done in this picture is I've put the two type comments next to
each other, and the two templates next to each other.
And I think that when I arrange it like this, you can see what's going on here a
little bit better. In the two tied comments: we have the
self reference from earlier this week, and we have the reference which we just
saw today for the first time. And in the templates, the soft reference
produces a natural recursion, and the reference produces what we're going to
call a natural helper. When we design a function that consumes a
list of school, we'll see what the presence of this natural helper in the
template does to the design of that function. [BLANK_AUDIO]