So now, we're going to design a function that operates on list of string.
And remember what I said on the last video.
We still don't have all the changes we need to the how to design function recipe
to do this. So, there's going to be a place in there
where I'm going to make a lucky guess. And that lucky guess is going to turn out
splendidly for me. And then in the next video, I'm going to
come back and really explain away all those lucky guesses, so that you can make
them, too. We're starting where we left off in the
last video. We have a data definition for list and
string. And that's the type that we're using to
represent lists of quidditch teams. Remember, there were three things about
this data definition that I said I would talk more about later, and that'll be in
the next video. One was this self reference here at the
top, well, is the fact that I put in this funny call to fun for los here.
And last thing is I didn't say what template rule caused me to put that fun
for los. Now if we go down a little farther in
this starter file, there's a second problem.
And the problem here is we want to know whether your list of favorite Quidditch
teams includes UBC. So, we're going to design a function that
consumes list of string and produces true if the list includes UBC.
So, this a how to design function problem.
So, let's just get going. Signature first, it's going to consume a
list of favorite quidditch teams. So, that's list of string.
That's how we're representing that. And it's going to produce true if the
list includes UBC. So, that means it's going to produce a
Boolean. And let's see.
We'll say produce true if los. We'll call it, includes UBC, and stub is
defined. Let's say we'll call this function
contains UBC, los. And we'll make the stub produce false.
We'll say that that's the stub. Now, let's see some examples.
We get some inspiration from the data examples that we have in the list of
string data definition. So, empty is definitely a list of
strings, let's try that. Let's say contains UBC of empty.
Check expect of that. Well let's see, does the empty list
contain ubc, well no it doesn't, so that's easy.
That's False. Here's another one.
Check expect contains ubc of cons McGill empty.
Well, now UBC's not in there either. Let's make one that UBC is in.
check-expect contains-ubc? cons UBC empty.
That's true. Let's make one that UBC is in, but not in
first position. Let's see, you can see who's in that
list, so that'll be true. So ,we'll run that and make sure those
examples are well formed, adn they are. Two are failing.
Now let's see, let's go get the template. And we'll comment off the stub.
We'll rename the template. Now it's time to code the body.
So, let's see. Contains UBC.
If los is empty, that's this first case, then intuition the purpose and this first
example all three tell me that the result in that case is false.
Now, what about this second case? So this is the case where, let's see,
it's compound. This is the case where we have an actual
cons. And this first of the cons will be a
string. So that's a case like this, where we have
McGill as the first element of the list. Or it's a case like this where we have
ubc as the first element on the list. So, in that case, we would produce false
and in that case it would produce true. So, lets' see it seems like or it could
be this McGill is in the beginning of the list, but we need to keep looking.
So, it seems like we need an f. And there's the f, and it seems like what
we want to know is, the first thing in the list UBC.
So, if the first thing in the list is UBC.
That's this case here where the first thing in list is UBC.
If the first thing in the list is UBC, then in that case we should choose true.
You know, what do we need to do here, what do we need to do in this case?
This is the case where the first thing in the list is not UBC.
So that's a case like this one, where the first thing on the list is not UBC, and
it's also a case like this one, where the first thing on the list is not UBC.
In this case, UBC isn't anywhere on the list.
But in this case, UBC is in the rest of the list.
So, it seems like what we need to do here is, we got the rest of the list.
It seems like we need to go look in the rest of the list to see whether the rest
of the list contains UBC. That way in this case, when we go look in
empty, there won't be UBC there. So, we'll produce false, and in this case
when we go look in cons UBC empty, we'll find the UBC and produce true.
So what we need right here, what we need fn for los to be, is we need fn for los
to be a function. And if we give it a list of strings, it
will tell us whether UBC is in that last. It would be nice to have such a function.
Well, we do. Or I should say, we almost do.
It says right here that there's a function that consumes list of strings
and produces a Boolean and it produces true if the list includes UBC.
And all of this says that the name of that function contains UBC.
So, since there is a function that does that it says right there, there is.
I can just make this the contains UBC, and now I'm done writing that function.
Let's run it. All four test passed, and you may find
that surprising. A funny thing happened here which is we
wrote a function that used itself. This idea, which is called recursion, is
deeply connected to the idea we saw in the last video which was self referenced
in the type comment. For now, I'm just going to leave this
video as it is, say that we're done doing this function.
And then in the next video, I'll talk about why this all worked out.