In this session, we are going to cover an important concept in functional

programming. Higher order functions let you pass

functions as arguments and return them as results.

One thing particular about functional languages is that they treat functions as

first class values. This means that.

Like any other value a function can be passed as a parameter to another function

or returned as a result. We'll see in this session that this

provides a flexible way to compose programs.

Functions that take other functions as parameters, or that return functions as

results are called higher order functions. So, that's the opposite of first order

functions. A first order function would be, then, a

function that acts on data types, simple data types such as [inaudible] or longs or

lists, but not other functions. Whereas, a higher order function acts on

other functions. Let's see an example.

Let's suppose we want to take the sum of all the integers between a and b.

So we could write something like this. Sum iints, it takes the, the bounds as

parameters, the and the b. And we ask well, if a is greater than b,

then there is nothing to sum. And we would return zero as the, logical

value for that. And otherwise we would return a + a

recursive call of sum ints of a+1 and b. Good.

Now that we've done that, let's vary the problem a little bit and consider taking

the sum of the cubes of all integers between a and b.

So here's the cube function. Take an x, return x times x times x.

Some cubes then would be the same as some ints.

But, where we used an A before now we return cube of A.

And otherwise, it's the same thing as some ints.

Recursive call to sum cubes in this case. Next, let's take the sum of all factorials

of the integers between a and b. Well, you see the principle by now.

The program, again, is exactly like sum ints and sum cubes, except that where

previously we computed the q, we compute the factorial.

Of course, these are all special cases of the mathematical sum of the values of f of

n, where f is the given function, and n is taken from the interval between a and b.

The questions is, well if mathematics has a special notation for that, shouldn't

programming have one as well. Can we factor out the common pattern into

a single method. So here's how that's done.

Let's define a function sum which takes a parameter F of type INT to INT, and to two

bounds A and B, both INTs. And let's generalize the previous, three

definitions as follows. We write, if A greater than B, then zero,

otherwise, take F of A plus sum of F and A plus one.

B. So the new thing here is that F is a

parameter of the sum function. It's not a given function.

It's a parameter. Once we have that, we can then write our

function sum in sum cube, sum factorial as follows.

Sum. Ints would be simply sum of id of ab,

where the id function simply returns its parameter unchanged.

Sum cubes would be sum of cube and AB, where the cube function is as what we've

seen before. And finally, sum factorial is sum of fact

and AB, where the fact function is, again, the factorial function.

So what we've done, effectively, is reuse the pattern that defines the sum function,

so that we had to write that only once. And we could reuse it in the three

definitions of the particular sums. One thing that's new here is a function

type. Function type is written A aero B, where A

and B are types. And its a type of a function that takes

some argument of type A, and returns a result of type B.

So for example Int aero Int, is a type of functions that map integers to integers.