In mathematics and computer science, a higher-order function (also functional, functional form or functor; not to be confused with the functor concept in category theory) is a function that does at least one of the following:

takes one or more functions as arguments,

returns a function as its result.

In the untyped lambda calculus, Higher order functions assume that for T1,T2,T3 functions a map of (T1->T2)->T2 is equivalent to T1->(T2->T3).

But how are these applicable in programming? With examples inspired from Functional Programming book authored by Martin Odersky I will demonstrated the beauty of higher order functions within the context of Functional programming.

I will use Scala since if found it more mathematical native and originates in the depth of lambda calculations. Scala has always been my programming language of preference when it comes to express mathematical thinking.

I will start with two examples developed in an imperative style and then improve the algorithm with higher order functions injection.

Example 1: Let’s write a program that calculate the sum of squares between two points a and b.

The function square f mapped to x is defined as X->f(x)=x*x and the sum of square between a and be will be (a,b)∑f(x) with f(x)=x*x where x evaluates to all points between a and b.

Let’s define then our two functions square and sumSquares

def square(x: Double): Double = x * x

def sumSquares(a: Int, b: Int): Double =

if (a > b) 0 else square(a) + sumSquares(a + 1, b)

if we call this function as sumSquares(2,3) it would return 13 as expected.

Example 2: Let’s write a program that computes the sum of square-roots between two points a and b

A common way to compute square roots is by Newton’s method of successive ap-

proximations. One starts with an initial guess y (say: y = 1 ). One then repeatedly

improves the current guess y by taking the average of y and x/y . As an example, the next three columns indicate the guess y , the quotient x/y , and their average for the first approximations of square root of 2.

1 2/1=2 1.5

1.5 2/1.5=1.333 1.4167

1.4167 2/1.4167=1.4118 1.4142

… …. …..

y x/y (y+x/y)/2

Let’s implement this algorithm in Scala

def sqrt(x: Double) = {

def sqrtIter(guess: Double): Double =

if (isGoodEnough(guess, x)) guess

else sqrtIter(improve(guess))

def improve(guess: Double) =

(guess + x / guess) / 2

def isGoodEnough(guess: Double, x: Double) =

abs(square(guess) – x) / x < 0.00000001 sqrtIter(1) } This function checks every time if our guess is good enough by doing a square of the guess minus the value we wanted to determine its square root. If the result is approximately zero then the value is good enough. Note that this function may not converge for big values or smaller values since the gap between the square and the original value might be big. By dividing the subtraction of the square of the guess and the value x by x we narrow the gap. def sumOfSquareRoot(a: Double, b: Double): Double = if (a > b) 0 else sqrt(a) + sumOfSquareRoot(a + 1, b)

By calling this function sumOfSquareRoot(2,3) we would get approximately 3 which is exactly what we expected.

If you observe the two examples there are similarities. Both functions use (a,b)∑f(x).

How can we generalise the call to (a,b)∑f(x) in order to have one function that handle both sum of squares and sum of square roots? That is where higher order functions become handy.

I will straight show what the higher order function for the above examples would look like without going into details on how the compiler will interpret it but if you observe the example carefully you will notice without difficulty that I am just applying lambda calculations theory.

def sum(f: Double => Double): (Double, Double) => Double = {

def sumF(a: Double, b: Double): Double =

if (a > b) 0 else f(a) + sumF(a + 1, b)

sumF

}

And our sum functions will be modified like this:

def sumSquares = sum(square) // sumSquares: => (Double, Double) => Double

def sumOfSquareRoot = sum(sqrt) // sumOfSquareRoot: => (Double, Double) => Double

I have added comments to show that both functions sumSquares and SumofSquareRoot take one parameter which is the function that evaluate the expression of what we want to evaluate for a specific point and then return a function that in turn takes two parameters of the current point and the maximum point. The inner function is called and called again until the function falls within a range where a > b.

As you can see Higher functions are very handy and easy to understand. Once you pick the logic up you will never get rid of them in your daily programming. You can build on the above example codes to compute the sum of points between a and b, the sum of 2 squared n with n representing the set of points between a and b. etc,… for the sake of exercising just try to expand my examples and let me know what you think.

Permalink

You actually expressed that superbly!

viagra unsuccessful entry pdf buy viagra soft

viagra 100mg

best buy viagra – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Fine material. Regards.

dog on viagra international language of viagra

buy viagra online

buy viagra in england – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Very well voiced truly. !

card com viagra robin william viagra

viagra for women

viagra 100mg tablet 4 pack – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Appreciate it! Lots of forum posts!

free viagra trial viagra nz

viagra generic

best price viagra cialis – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Really a good deal of fantastic data!

spam email containing viagra url mdma viagra combo on the street

viagra pills

high from viagra – https://genericviagramsl.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Truly all kinds of useful data.

3 generic meltabs viagra has anyone used viagra

viagra for women

viagra scottland – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

You reported that well!

viagra without buy prescription extra strong viagra

generic viagra

generic viagra sildenafil – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

This is nicely put. !

how to get viagra in philippines vision problems resulting from viagra

viagra without a doctor prescription

cialis viagra sampler – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Nicely put. Cheers!

viagra in neonates super viagra beads

viagra generic

buying viagra online vs doctor – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Thanks a lot, Lots of stuff.

viagra photos viagra rep

viagra pills

best buy viagra – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

This is nicely expressed. .

more than one viagra day viagra sweden

generic viagra

viagra prescription canada – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

You actually revealed this well.

free viagra samples before buying cialis levitra link pharmacies com viagra

buy viagra

can a psychiatrist prescribe viagra – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

You have made your position pretty effectively!.

is viagra a prescription drug wikipedia viagra ginko

viagra pills

non persription viagra – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Thanks. I like this!

online pharmacy prescription drug viagra prescription female viagra

buy viagra online

viagra violence – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Nicely put. With thanks!

how long will viagra last keywords viagra mp3

cheap viagra

viagra user experiences – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Seriously lots of wonderful tips.

where does viagra spam come from viagra and effects and healthy males

generic viagra 100mg

insurance for viagra – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Helpful knowledge. Cheers.

smell sensitivity with viagra plantiffs gets settement for viagra

viagra generic

buy 100 mg viagra – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Useful information. Thank you.

online sellers of cialis and viagra viagra without prescription mississauag

viagra 100mg

how safe is viagra from india – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Really loads of superb info!

law suits against viagra viagra medicinal

cheap viagra

when will viagra go generic – https://genericviagramsl.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Regards! I enjoy this.

increase libido female viagra overight delivery viagra

generic viagra online

viagra strength of erection – https://genericviagramsl.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Nicely put. Thanks a lot!

online viagra buy viagra online tadalis whats in herbal viagra

cheap viagra

viagra ireland – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Wow lots of helpful info!

canadain online pharmacies viagra can a psychiatrist prescribe viagra

generic viagra 100mg

pfizer viagra online – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Valuable information. Regards!

hentai viagra viagra sweden

buy generic viagra

men using viagra sex tubes – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Cheers! I like it.

dangers of taking too much viagra does womens viagra work

cheap viagra

when does viagra typically not work – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Fantastic write ups, Kudos!

white rabbit and viagra viagra in germany

viagra for women

natural substitute for viagra – https://genericviagramsl.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Thanks, Lots of postings.

viagra and loss of vision canadan viagra

viagra pills

robin williams viagra video – https://genericviagramsl.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

You said it adequately..

is viagra safe with depresion medication ayurve viagra

generic viagra online

drug stores that sell viagra – https://genericviagramsl.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

You said it really well.

when to take viagra before sex viagra without a perscripton legal

viagra pills

compare levitra and viagra effects – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Nicely put. Many thanks!

andy griffith viagra find viagra edinburgh pages search

viagra for sale uk

mixing wellbutrin and viagra – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Useful postings. Cheers!

viagra on girls women who use viagra

generic viagra 100mg

common viagra dose – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Kudos, I enjoy it!

recreational uses of viagra online viagra without a prescription

buy viagra online

substitute for viagra – https://genericviagramsl.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Very good information. Cheers!

patent how long viagra viagra order cheap

viagra online

eli lily viagra patent – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Useful forum posts. Cheers.

viagra emails from your own address viagra kamagra cialis aneros

generic viagra online

free sample viagra womens – https://genericviagramsl.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

You actually stated it really well!

can women take viagra of cialis viagra sample dallas

viagra pills

blood pressure medications simular to viagra – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

You said it nicely.!

alternatives to viagra viagra coagulation

buy viagra

pfizer viagra uk – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Fine material. Cheers.

maker of viagra order viagra online no rx prescription

generic viagra

viagra treatment for crohns – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Reliable postings. Cheers!

viagra heart disease when viagra does not work

generic viagra online

discount viagra perscription drug – https://genericviagramsl.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Wonderful posts. Appreciate it.

3 caverta veega generic viagra forced panty submission viagra

viagra generic

viagra use may damage fertility videos – https://genericviagramsl.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

You actually suggested it wonderfully.

united states classaction on viagra german viagra

viagra 100mg

buy viagra in london – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Incredible all kinds of very good advice!

female enhancement liquid version viagra niagra find search viagra ago edinburgh hours

viagra without a doctors prescription

kids on viagra – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Great content, Many thanks.

viagra latest side effects professional viagra discussions b ogs

buy viagra

viagra and blindness – https://genericviagramsl.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Beneficial advice. Kudos.

nitroglycerin ointment viagra don’t take viagra with

viagra for women

viagra genaric – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Terrific content, Many thanks!

viagra carvedilol interaction discount viagra in the usa

viagra for sale uk

viagra proffessional – https://genericviagramsl.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Thanks. Great information.

viagra on females semen turns yellow viagra

generic viagra

wal mart pharmacy viagra price – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

You actually suggested it really well!

viagra levitra cialis ads viagra ad on rush limbaugh

cheap viagra

viagra news edinburgh comment search – https://genericviagramsl.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Seriously loads of helpful material.

acumar and viagra is viagra safe

buy generic viagra

should every man take viagra – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Wonderful advice. Many thanks!

buy discreet medical viagra drugs cheating viagra best results pharmacy

generic viagra online

online viagra india – https://genericviagramsl.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Nicely put, Regards!

buy viagra at a discount viagra gel aka james bond

generic viagra 100mg

viagra paxil lasting – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

You stated this perfectly.

supreme suppliers viagra viagra on medicaid

generic viagra online

viagra siesta – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

Amazing facts. With thanks.

buy drug generic generic online viagra viagra and also order viagra online

viagra generic

viagra use – https://genericviagrantx.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

With thanks! Great stuff.

cheap viagra forum women consume viagra

generic viagra 100mg

online viagra scams – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty

Permalink

You made your point very clearly!!

viagra use viagra in holland

viagra for women

cheap viagra online – https://genericviagrarcp.com

KeithPrice

RichardGaile

Ernestojarty