Tip:
Highlight text to annotate it
X
This segment and the next couple, which are all optional fit together and are
about taking some of our closure idioms and porting them to languages that don't
actually have closures. So the idea here is that closures and higher-order
programming, Programming with functions like map and filter is great. And it's
particularly easy when your language supports closures. Because you can very
easily create things with private data, pass them around. Function types are
exactly what we want. But if your language doesn't have closures you can still
program in this style. Now it's often much more painful. You often have to create
what conceptually is your closure environment manually. But by showing you
how to do this in languages you might be familiar with we can see some of the
connections between different programming styles. So one segment is gonna do this in
an object oriented style using java, and the key idea there is to have interfaces,
that just have one method, so that's kind of like the function code, the call me.
Part of an interface of, of a closure. And then in C, where we have function pointers
but we don't have closures, we can pass the environment explicitly as an extra
argument. So these are optional segments because you need to be a bit of an expert
in Java to understand the Java one. And I bit of an expert in C to understand the C
one. Even if you do know some Java or C, you may find this a bit over your head,
that's okay. I wanna put these segments up, so you can refer back to them someday.
If you're in these languages, and you find yourself wanting to program in a
functional style, then these segments might help you, point you in the right
direction. So it shows some connections between the languages and the features It
can help you understand what closures and objects are really all about, and at the
end of the day it is a bit clumsy so it may make you just wish that more
programming languages actually had closures, so that we didn't have to
simulate them and incode the same idioms. In more painful ways. So this segment just
shows the ML code that we're then going to translate, which is often called porting,
to Java or C. It's just a little list library. I'm not going to use ML's built
in lists because I want to show all the code, right? And I want to find a fuller
comparison because we're going to write everything ourselves in all three
languages. And then the next segments are independent. You can watch one or the
other or both or neither And then show how to write the same code in those other
languages. So here we are. I'm just gonna define my own polymorphic data
constructor. Alpha my list, two constructors. Cons which has two parts,
the head of the list, which is an alpha. And the tail of the list, which is an
alpha my list. Or empty for the empty list. These are just plain old
constructors. So I can write a function map. I'm using a curried style here, but
that's not essential. That takes in a function and one of these my lists, so not
ML's built in lists but one of these my lists. If it's empty we return empty. If
it's not empty then we cons on F applied to X and mapping F across Xs. That is
mapped over the my list type. Filter is similar, it's just the appropriate if then
L. I just want to expand this out here so you can see it. Indent it a little more
nicely. Empty list is empty list, if f of x, then Cons x onto filtering f across
x's, otherwise ignore x and just filter f across x's. And finally, length, which is
easier than any of them. Empty list returns zero, non empty list Returns one
plus the length of the rest of the list. Then a couple clients that are using these
functions to define their own more specific functions. How about something
that takes a My List of integers and doubles all the elements. Then we would
just use the map function that's no longer quite on the screen, but is above.
Remember two curried arguments So I'm doing a partial application here. I'm just
calling it with, an anonymous function that takes a number, and multiplies it by
two. And then, how about another function t hat
just takes a, a list. A my list, of course. And, in N. and returns how many.
elements of the list are n, okay? And so, one way I could do this in terms of the
functions I've written, it's not the most efficient way to compute this but it does
work, is to first filter the list. They so that we only have the elements that are n.
And then compute its length. Alright? So the result of this called a filter will be
a list that has nothing in it but a bunch of the number n. Then however long that
number, that list is, is the number of n's that were in the original list. So we're
gonna write that code in this style, in Java, and C in the upcoming segments.