Higher-order programming language is a programming style or approach in which, functions, object or modules can be used as values. This is a style used commonly with Functional programming. However this is not limited to functional programming only. It can be used with Object-oriented programming languages also. And Ruby is a very good example of this. Other programming languages which support, Higher Order programming are, Python, Java, JavaScript, Perl, Prolog, Scala and many more.

Properties

edit

Below are the important properties of any higher order programming language.

Higher Order Function

edit

There are two kinds of functions. Single Order and Higher-order function. Single ordered functions do not receive function as an argument and do not return a function as a result. However, higher order functions, can receive function as an argument and/or return a function as a result. The number of inputs and output functions will decide the order of the function.


Map is a very common example of a higher order function. It takes a list of elements as input and returns a list as output. Operations will be performed on each element of the input list individually.

Ruby

a = Admin.all
@admin_emails = a.map { |admin| admin.email }

Here a gets the list of all the users from the Admin database and this is given to the map function which in turn performs operations on each element( In our case it is returning each admin’s email) and then returns the list of admin emails to the instance variable admin_emails.

Java Another example in Java where user wants to manage heights in different units like feet, cms and inches.

public class Scale {
        public String toFeet(int height) {
                return "your height is " + height + " feet";
        }
        public String toCentimeters(int height) {
                return "the height is " + height + " cms";
        }
        public String toInches(int height) {
                return "the height is " + height + " inches";
        }
}

Now user can add a Function in our Printer class’ print method:

    import java.util.function.Function;
    public class Printer {
            public void print(int height, Function<Integer, String> scale) {
                    System.out.println("Printing : " + scale.apply(height));
            }
            public static void main(String[] args) {
                    Printer measure = new Printer();
                    Scale scale = new Scale();
                    measure.print(6, scale::toFeet);
                    measure.print(165, scale::toCentimeters);
                    measure.print(75, scale::toInches);
            }
    }

Immutable Data

edit

Higher Order programming prefers to create a copy of the original data and make modification on that copy. This is done prevent the original data from being modified. This means that the value of the original data is not to be changed. It can be implemented by maintaining copies of the data and working on those copies. Immutable data is used to protect the integrity of the data. Eg: JAVA In JAVA, the final keyword can be used to make a variable immutable.

import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		final int i = 10;
		int j= i;
		j=11;
		System.out.println(j);
	}
}

To use the value of final integer “i” we make a copy of its value in “j” and then can perform operations on it. If we try to change the value of “i” we will get an error.

Referential Transparency

edit

This property deals with the fact that, each time a computation is made, it will give the same result. And this can be used to chain the method calls. This property denotes that the return value of a function is always same no matter how many times we execute it. It introduces the concept of equational reasoning which compares if two blocks of code are similar to each other. For this we will have to take a few factors under consideration like if the block of code is dependent on its context. If so, we will not be able to replace it with another code block. Eg: The most common example will be mathematical functions like sin(x), cos(x) etc How many ever times we call sin(x), it will return the same value for each x. However, assignments are not referentially transparent.

 y = y * 2; 

This will double the value of y every time it is called.

Lazy Evaluation

edit

Lazy evaluation does exactly opposite to what Eager/Early evaluation does. In early evaluation, the function arguments are evaluated before the function is executed. In lazy evaluation, this is exactly opposite. The arguments are evaluated only if they are required by the calling function and will be executed when their execution is must. This is done to prevent ahead of time execution. This involves the execution of certain portions of code when it is needed. Eg: Python In Python 2.x we have the range() function which computes a list of integers. The entire list is stored in memory when the first assignment statement is evaluated, so this is an example of immediate evaluation:

 
 >>> rng = range(10)
 >>> print rng
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 >>> print rng[5]
 5

Where as in Python 3.x the range() function returns a special range object which computes elements of the list on demand. Elements of the range object are only generated when they are needed (e.g., when print(rng[3]) is evaluated in the following example), so this is an example of lazy or deferred evaluation:

 >>> rng = range(10)
 >>> print(rng)
 range(0, 10)
 >>> print(rng[5])
 5

This change to lazy evaluation will save execution time for large ranges which may never be fully referenced and memory usage for large ranges where only one or a few elements are needed at any time. .NET Framework The .NET framework has a predefined class to implement Lazy Evaluation. It is System.Lazy<T>. This class can be directly used as follows:

public int Product()
{
    float a = 0;
    float b = 0; 
    Lazy<float> c = new Lazy<float>(() => a * b);
    a = 3;
    b = 5;
    return c.Value;      // returns 15
}

Recursion

edit

A functional which makes a call to itself is known as recursive function and this is known recursion. Recursion is implemented in almost all the programming languages but it plays a very important role in higher order programming languages as in many languages this is the only way a loop/iteration works.[1] As this being a very critical component, tail call optimization is implemented.

Summary

edit

Higher Order Programming is growing in popularity among many commercial setups and in academia as well. As it involves working with pure functions, the concept of referential transparency to build abstractions of smaller ones can be used, Is is possible to organize the code even better with the help of modules. It gives one the ability to reason about program using the equational reasoning concept. On the whole it helps one to write elegant code.

See Also

edit

References

edit