Archive

Posts Tagged ‘declarative programming’

Functional programming and Declarative programming


Imperative programming paradigm contains the description of each step need to be performed to solve a problem. This approach is also referred as an algorithmic approach to solve a given problem. Most well-known languages like c, c++ or java are imperative type programming languages.

Functional programming is implementation of a certain task in the form of composition of different functions.

In a very simple terms, we can understand the difference between the above two as Imperative languages describe “how to” accomplish a goal and functional programming describes “what to” aspect of it. That means imperative programming languages contain loops, conditional statements, methods whereas, functional paradigm contains functions (recursions).

The main difficulty to transition to the functional programming is many are quite familiar with the declarative method of thinking style. Functional programming requires a different set of mind set to describe the problems in the form of functions.

Many imperative languages (such as C++11) are providing ways and means to add the support for the functional programming paradigm to their language tool kit. This allows the developers to easily make use the best of the both worlds.

 

The following are very few language constructs provided by C++ language (C++ 11) to facilitate the functional programming paradigm.

auto key word:

ex: auto a = 10;


#include <iostream>
#include <stdio.h>
using namespace std;

int main(void)
{
	auto n = 10;
	cout << n << endl;
	getchar();
}

Output: 10

#include <iostream>
#include <stdio.h>
using namespace std;

int main(void)
{
	auto str = "Hi!";
	cout << str << endl;
	getchar();
}

Output: Hi!

The above code makes declaring a variable much cleaner and much easier.

Lambda functions: (Source from http://en.cppreference.com/w/cpp/language/lambda)

Characteristics:

  1. Unnamed function object
  2. Captures scope in its closures

Syntax:

[ capture-list ] ( params ) mutable(optional) exception attribute -> ret { body }

[ capture-list ] ( params ) -> ret { body }

[ capture-list ] ( params ) { body }

[ capture-list ] { body }

1) Full declaration.

2) Declaration of a const lambda: the objects captured by copy cannot be modified.

3) Omitted trailing-return-type: the return type of the closure’s operator() is determined according to the following rules:

§  if the body consists of nothing but a single return statement with an expression, the return type is the type of the returned expression (after lvalue-to-rvalue, array-to-pointer, or function-to-pointer implicit conversion);

§  otherwise, the return type is void.

(until C++14)
The return type is deduced from return statements as if for a function whose return type is declared auto. (since C++14)

4) Omitted parameter list: function takes no arguments, as if the parameter list was ().

mutable – allows body to modify the parameters captured by copy, and to call their non-const member functions
exception – provides the exception specification or the noexcept clause for operator() of the closure type
attribute – provides the attribute specification for operator() of the closure type

capture-list – a comma-separated list of zero or more captures, optionally beginning with a capture-default.
Capture list can be passed as follows (see below for the detailed description):
 [a,&b] where a is captured by value and b is captured by reference.
 [this] captures the this pointer by value
 [&] captures all automatic variables odr-used in the body of the lambda by reference
 [=] captures all automatic variables odr-used in the body of the lambda by value
 [] captures nothing
params – The list of parameters, as in named functions, except that default arguments are not allowed(until C++14). if auto is used as a type of a parameter, the lambda is a generic lambda (since C++14)
ret – Return type. If not present it’s implied by the function return statements ( or void if it doesn’t return any value)
body – Function body

#include <iostream>
#include <stdio.h>
using namespace std;

int main(void)
{
	auto min = [](int a, int b) { return a < b ? a : b; };
	cout<< min(12, 34) << endl;
	getchar();
}

Output: 12

In the above example: the closure is empty that means it is not capturing anything but only two parameters are passed, this function takes a and b and returns the minimum.

Example: Caputure by value


#include
#include
using namespace std;

#include <iostream>
#include <stdio.h>
using namespace std;

int main(void)
{
	int divisor = 10;
	auto divideAllSum = [divisor](int a, int b) { return a / divisor + b / divisor; };
	cout<< divideAllSum(30, 40) << endl;
	getchar();
}


In the above example divisor was captured by value hence this value cannot be changed inside the lambda function.
Output: 7
Example: Caputure by reference


#include <iostream>
#include <stdio.h>
using namespace std;

int main(void)
{
	int divisor = 10;
	auto divideAllSum = [&divisor](int a, int b) { divisor = 5;  return a / divisor + b / divisor; };
	
	cout<< divideAllSum(30, 40) << endl;
	cout << "divisor : " << divisor << endl;
	getchar();

}

Output:
14
divisor : 5
In the above example no matter what the value being sent to the function the function is changing it to 5 and returning the value.
Capture all the scope as reference:

#include <iostream>
#include <stdio.h>
using namespace std;

int main(void)
{
	int divisor = 10;
	auto divideAllSum = [&](int a, int b) { divisor = 5;  return a / divisor + b / divisor; };
	
	cout<< divideAllSum(30, 40) << endl;
	cout << "divisor : " << divisor << endl;
	getchar();

}

In the above case entire the calling function scope has been captured as a reference hence it is possible to modify the divisor variable to 5.
Capture whole scope as a value:

#include <iostream>
#include <stdio.h>
using namespace std;

int main(void)
{
	int divisor = 10;
	auto divideAllSum = [=](int a, int b) { return a / divisor + b / divisor; };
	cout<< divideAllSum(30, 40) << endl;
	getchar();

}

In the above example “=” is part of closure signifies that the whole scope has been passed as a value hence can use all the variables part of the calling scope.

Using lambda functions as an argument to functions:


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

int main(void)
{
	vector<int> l;
	for (auto i = 11; i >= 0; i--)
		l.push_back(i);
	sort(l.begin(), l.end(), [](int a, int b) {
		return a >= 5? (a < b) : a > b;
	});
	auto print = [](vector<int> myl) {for (auto i = myl.begin(); i != myl.end(); i++) cout << *i << " "; };
	print(l);
	getchar();
}

Output:
5 6 7 8 9 10 11 4 3 2 1 0

In the above example:
To sort function the lambda function is passed as our own comparator.

Advertisements