0% found this document useful (0 votes)
82 views6 pages

Declarative Programming Paradigm: Functional Programming

The document provides an introduction to functional programming concepts including lambda calculus, higher order functions, and monads. Key points covered include: - Lambda calculus forms the theoretical foundation of functional programming and involves lambda functions and variable binding/substitution. - Functional programming uses mathematical functions and avoids variables/state changes. Functions always produce the same output for the same inputs. - Higher order functions take other functions as arguments or return functions. They are important for functional composition. - Monads combine functions to appear as a single function by wrapping values in boxes and passing them between functions. They are key to functional programming.

Uploaded by

Pravin Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
82 views6 pages

Declarative Programming Paradigm: Functional Programming

The document provides an introduction to functional programming concepts including lambda calculus, higher order functions, and monads. Key points covered include: - Lambda calculus forms the theoretical foundation of functional programming and involves lambda functions and variable binding/substitution. - Functional programming uses mathematical functions and avoids variables/state changes. Functions always produce the same output for the same inputs. - Higher order functions take other functions as arguments or return functions. They are important for functional composition. - Monads combine functions to appear as a single function by wrapping values in boxes and passing them between functions. They are key to functional programming.

Uploaded by

Pravin Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

Module 3

Declarative Programming Paradigm: Functional


Programming
Introduction to Lambda Calculus, Functional Programming Concepts,
Evaluation order, Higher order functions, I/O- Streams and Monads.

Self-learning Topics: Implementation of I/O using any programming language.

Declarative programming is a programming


paradigm … that expresses the logic of a computation
without describing its control flow.

Imperative programming is a programming


paradigm that uses statements that change a program’s
state.

Functional Programming Languages:


- The design of the imperative languages is based directly on the von Neumann architecture
- Efficiency is the primary concern, rather than the suitability of the language for software
development.
- The design of the functional languages is based on mathematical functions
- A solid theoretical basis that is also closer to the user, but relatively unconcerned with the
architecture of the machines on which programs will run
Mathematical Functions:
Def: A mathematical function is a mapping of members of one set, called the domain set, to another
set,
called the range set.

F(x)=x%2
X={2,3,4,5,6,7} r=(0,1,0,1,0,1)

2 0

3 1

Fundamentals of Functional Programming Languages:


- The objective of the design of a FPL is to mimic mathematical functions to the greatest extent
possible
- The basic process of computation is fundamentally different in a FPL than in an imperative language
- In an imperative language, operations are done and the results are stored in variables for later use
- Management of variables is a constant concern and source of complexity for imperative
programming
- In an FPL, variables are not necessary, as is the case in mathematics
- In an FPL, the evaluation of a function always produces the same result given the same parameters
- This is called referential transparency

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)

Applications of Functional Languages:


o APL is used for throw-away programs
o LISP is used for artificial intelligence
o Knowledge representation
o Machine learning
o Natural language processing
o Modeling of speech and vision
o Scheme is used to teach introductory

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.

The Lambda calculus is an abstract mathematical theory of computation,


involving \lambdaλ functions. The lambda calculus can be thought of as the theoretical
foundation of functional programming. It is a Turing complete language; that is to say,
any machine which can compute the lambda calculus can compute everything a Turing
machine can (and vice versa).

The \lambdaλ notation is based on function abstraction and application based on


variable binding and substitution.

e.g Expression

F(x)= x%2

Parameter

Parameter

Lambda Abstraction
x.x+1

Expression

Function Signifier

x x%2

(lambda hide working process and gives only result)

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

x. x*1 ( x.x*1)3  x*1=3

 Need of Lambda
It is Universal mode of computation which can be encode anything.

Functional Programming language that supports lambda calculus are


LISP, Haskell, ADA etc.

Everything is in the form of function only get more abstraction.

Help us to increase our understanding of computation.

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

 Free and Bound Variables


Values are going to substituted for bounded variable only and not for free variable.
x. x+y
Free Variable
Bounded
Veriable

E.g
( x. x+y) 3  3+y

e.g 2 Two variables


((( x. y. x+y+z)3 ) 4)

( y. 3+y+z)4
3+4+z 7+z

 Higher Order Function


Higher-order functions are used a lot in functional programming. They are
functions that take other functions as arguments.
One of the great advantages of using higher order functions when we can
create smaller functions that only take care of one piece of logic. Then, we
compose more complex functions by using different smaller functions. This
technique reduces bugs and makes our code easier to read and understand.

Higher-order function is an essential part of the functional programming

paradigm. We must have defined a lot of functions in any language where we

pass either primitive types or an object as an argument and returns the same.

These are normal functions or methods.

Then comes first-class functions. First-class functions are the functions that are

treated as values. It means, those functions can be assigned to a variable and

can be passed around as an argument.

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 a particular style of combinator. It is the key to Functional programming.


Imperative programmers mostly ignore it or sometimes even feared of it. When we talk about
the monads, the lot of fancy terminology from category theory that gets thrown around.
However the concepts of category theory are irrelevant to what winds up being a simple and
well understood functional programming concept.

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.

You might also like