User:Bhargav jhaveri/sandbox

 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
Below are the important properties of any higher order programming language.
 * Higher-order function
 * Purity
 * Immutable Data
 * Referential Transparency
 * Lazy evaluation
 * Recursion

Higher Order Function
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 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.

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

Immutable Data
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.

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
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
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: 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:

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. This class can be directly used as follows:

Recursion
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. As this being a very critical component, tail call optimization is implemented.

Summary
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.