Declarative Programming Paradigm: Functional Programming
Declarative Programming Paradigm: Functional Programming
F(x)=x%2
X={2,3,4,5,6,7} r=(0,1,0,1,0,1)
2 0
3 1
1.Function Composition:
A functional form that takes two functions as parameters and yields a function whose result is a
function whose value is the first actual parameter function applied to the result of the application of
the
second Form: h(f )° g which means h (x) f ( g ( x))
2. Construction:
A functional form that takes a list of functions as parameters and yields a list of the results of applying
each of its parameter functions to a given parameter
Form: [f, g]
For f (x) = x * x * x and g (x) = x + 3,
[f, g] (4) yields (64, 7)
Lambda Calculus
The lambda calculus was introduced by mathematician Alonzo Church in the 1930s as part
of an investigation into the foundations of mathematics. Lambda calculus is a formal system in
mathematical logic for expressing computation based on function abstraction and application
using variable binding and substitution.
e.g Expression
F(x)= x%2
Parameter
Parameter
Lambda Abstraction
x.x+1
Expression
Function Signifier
x x%2
e.g 2
F(x,y)=x*y
x. y.x*y x x*y
y
Substitution
F(x)=x*1 f(3)= x*1 3
Need of Lambda
It is Universal mode of computation which can be encode anything.
Syntax
1. Variable E::= x Parameter
2. Function Creation x. E Expression
3. Function Application
E1,E2
Where E1 is calling function with argument E2.
E.g
( x. x+y) 3 3+y
( y. 3+y+z)4
3+4+z 7+z
pass either primitive types or an object as an argument and returns the same.
Then comes first-class functions. First-class functions are the functions that are
These are the functions that either takes one or more function as an argument
or returns the function as a result.
e.g
Higher Order Function
import java.util.*;
class HOF
{
public static void main(String args[])
{
String names[] = {"One","Acd","Abc","BCD"};
Arrays.sort(names, (String a, String b) -> {
return a.compareTo(b);
});
System.out.println(Arrays.toString(names));
}
}
What is Monad
Dictionary meaning of a Monad is a single unit but, in functional world Monad is termed as a
combinator which combines different functions such that it appears to be a single.
Monad is just like wrapping a value inside a box, unwrapping the value from the box and
passing it the function to get the wrapped value.
First approach
float x, y, z;
y = g(x);
z = f(y);
Second approach
The steps can be combined if we don't give a name to the intermediate result:
z = f( g(x) );
The first approach is known as Function Chaining, in which the output of first function goes
to the input of second function whereas, the second approach is known as Function
Composition. function composition uses Combinators to combine different functions.