# A Quick View on Higher order functions

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.

1. Живете в загородном доме и используете автономную канализацию! Тогда Вам точно нужен хороший фекальный насос! Как выбрать и где купить насос фекальный?
Подскажем вам один из лучших интернет-магазин насосов и фильтров, в котором Вы можете выбрать и купить лучший фекальный насос:
– фекальный насосы –
https://7filtrov.shop/catalog/nasosy/nasosy-fekalnye/ – насос фекальный режущий,
– дренажный
– шламовой насос
Это магазин – https://7filtrov.shop/catalog/
Широкий выбор различных товаров для коттеджа, с доставкой по Москве, Московской области и по всей России. Здесь Вам предлажат в том числе лучшие фекальные насосы по недорогим ценам.
Фекальные насосы, представленные в интернет-магазине от ведущих мировых производителей – Джилекс, Pedrollo, Leo, Vodotok – имеют официальную гарантию от производителя.
Представляем лучшие фекальные насосы –

https://7filtrov.shop/catalog/vodootvedenie_i_kanalizatsiya/nasosy_drenazhnye/3022/ – IBO IP 400 – купить фекальный насос,

https://7filtrov.shop/catalog/vodootvedenie_i_kanalizatsiya/nasosy_fekalnye/3067/ – IBO WQ 1300 PROFESSIONAL – купить фекальный насос,

https://7filtrov.shop/catalog/vodootvedenie_i_kanalizatsiya/nasosy_fekalnye/2195/ – Насос фекальный “LEO” 65WQ15-10-1.1 – купить фекальный насос,

https://7filtrov.shop/catalog/vodootvedenie_i_kanalizatsiya/nasosy_fekalnye/2173/ – Насос фекальный “Vodotok” WQ1100SQG – купить фекальный насос,
https://7filtrov.shop/catalog/vodootvedenie_i_kanalizatsiya/nasosy_fekalnye/2172/ – Насос фекальный “Vodotok” WQ1500DF – купить фекальный насос,

https://7filtrov.shop/catalog/vodootvedenie_i_kanalizatsiya/nasosy_fekalnye/5569/ – Насос фекальный UNIPUMP FEKAMAX 25-15-2,2 – купить фекальный насос.
Источник –
http://7filtrov.shop/catalog/nasosy/nasosy-fekalnye/ – фекальный насос для дома,
https://www.7filtrov.shop/catalog/nasosy/nasosy-fekalnye/ – купить дренажно фекальный насос,
https://vk.cc/aC3KsT – фекальные насосы для канализации с измельчителем.

2. Живете в загородном доме и используете автономную систему канализации! Тогда Вам точно необходим долговечный фекальный насос! Как выбрать и где купить насос фекальный?
Подскажем вам отличный магазин насосов для дома и дачи, в котором Вы можете выбрать и купить любой насос:
– фекальный насосы –
https://7filtrov.shop/catalog/nasosy/nasosy-fekalnye/ – фекальный насос отзывы,
– для скважины
– шламовой насос
Это магазин – https://7filtrov.shop/catalog/
Огромный выбор различных товаров для коттеджа, с доставкой по Москве, Московской области и по всей России. Здесь Вам предлажат в том числе лучшие фекальные насосы по доступным ценам.
Фекальные насосы, представленные в интернет-магазине от ведущих мировых производителей – Grundfos, Unipump, Джилекс – имеют официальную гарантию от производителя.
Представляем лучшие фекальные насосы –

https://7filtrov.shop/catalog/vodootvedenie_i_kanalizatsiya/nasosy_fekalnye/3074/ – IBO BIG 2200 (380В) (без поплавка) – купить фекальный насос,

https://7filtrov.shop/catalog/vodootvedenie_i_kanalizatsiya/nasosy_fekalnye/3051/ – IBO V 370 – купить фекальный насос,

https://7filtrov.shop/catalog/vodootvedenie_i_kanalizatsiya/nasosy_fekalnye/3081/ – IBO ZWQ 5500 (380В) – купить фекальный насос,

https://7filtrov.shop/catalog/vodootvedenie_i_kanalizatsiya/nasosy_fekalnye/2184/ – Насос фекальный “Vodotok” V1100F – купить фекальный насос,

https://7filtrov.shop/catalog/vodootvedenie_i_kanalizatsiya/nasosy_fekalnye/2169/ – Насос фекальный “Vodotok” WQD550F – купить фекальный насос,

https://7filtrov.shop/catalog/vodootvedenie_i_kanalizatsiya/nasosy_fekalnye/5566/ – Насос фекальный UNIPUMP FEKAMAX 80C2-1.5 – купить фекальный насос.
Источник –
http://7filtrov.shop/catalog/nasosy/nasosy-fekalnye/ – купить насос фекальный погружной,
https://www.7filtrov.shop/catalog/nasosy/nasosy-fekalnye/ – купить фекальный насос в москве,
https://vk.cc/aC3KsT – фекальный насос с измельчителем.