0% found this document useful (0 votes)
32 views14 pages

Declarative Style of Programming: Prescribing How It Should Do It

The document discusses declarative and functional programming. Declarative programming emphasizes describing what a program should do without prescribing how. Functional programming uses functions as basic building blocks and treats computation as function evaluation. Key principles of functional programming include immutable data, higher-order functions, and lambda calculus. Common data structures are lists and persistent data structures share memory between original and modified versions. Standard higher-order functions like map, filter, and reduce are used to transform and combine functions and data.

Uploaded by

Kumar Gaurav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views14 pages

Declarative Style of Programming: Prescribing How It Should Do It

The document discusses declarative and functional programming. Declarative programming emphasizes describing what a program should do without prescribing how. Functional programming uses functions as basic building blocks and treats computation as function evaluation. Key principles of functional programming include immutable data, higher-order functions, and lambda calculus. Common data structures are lists and persistent data structures share memory between original and modified versions. Standard higher-order functions like map, filter, and reduce are used to transform and combine functions and data.

Uploaded by

Kumar Gaurav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Declarative style of programming

Declarative Style of programming emphasis is placed on describing what a


program should do rather than
prescribing how it should do it.
Functional programming - good
illustration of the declarative style of
programming.
A program is viewed as a function from
input to output.

Functional style of programming


The function transforms the input into
output .
Functions are the basic building blocks
from which programs are constructed.
The definition of each function specifies
what the function does.
It describes the relationship between the
input and the output of the function.

Principles of Functional Programming


Treats computation as evaluation of mathematical
functions (and avoids state)
In functional programming, functions have same
status as variables; they are first-class entities.
They can be passed as arguments in a call.
They can transform other functions.
Higher Order Functions

higher-order functions: functions that operate on, or


create, other functions
functions as components of data structures

Lamda calculus provides a theoretical framework


for describing functions and their evaluation

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
A lambda expression specifies the
parameter(s) and the mapping of a
function in the following form
(x) x * x * x
for the function cube (x) = x * x * x

Mathematical functions
Lambda expressions describe
nameless functions
Lambda expressions are applied to
parameter(s) by placing the
parameter(s) after the expression
e.g. ((x) x * x * x)(3)
which evaluates to 27

Mathematical functions
Functional Forms
Def: A higher-order function, or
functional form, is one that either takes
functions as parameters or yields a
function as its result, or both

Example
A function that operates on other functions
is called a functional form. e.g.,
g(f, [x1, x2, ]) = [f(x1), f(x2), ],
becomes
g(Square, [2, 3, 5]) = [4, 9, 25]

Meaning: function g takes as


parameters another function and a
list (sequence) of values to which the
parameter function is applied.

Functions in FP languages
Given a set of input values, a function produces an output
value
Given the same input values, a function always produces
the same output value
Functions can use only the information provided by
their parameters
This excludes functions that return the date, the
time, or a random number
Functions have no side effects
Functions dont do input or output
Functions dont change the values of any variables or
data
Functions only return a value
8

Consequently, functions are easier to reason


about
To understand a function, you need examine
only the function itself
A function can use other functions, and of
course you need to know what those
functions are supposed to compute (but
nothing about how they do it)
In addition, functions can be called in any
order, including in parallel

Immutable and persistent data


structures
An immutable data structure is one that,
once created, cannot be modified
Immutable data structures can (usually) be
copied, with modifications, to create a new
version
The modified version takes up as much
memory as the original version
If all data is immutable, we may need
infeasible amounts of memory

10

A persistent data structure is one that, when


modified, retains both the old and the new
values
Persistent data structures are effectively immutable,
in that prior references to it do not see any change
Modifying a persistent data structure may copy part
of the original, but the new version shares memory
with the original

We generally talk about functional


programming as having immutable data, but
really its persistent

Lists
Lists are the original persistent data structures, and are
very heavily used in functional programming
[w, x, y, z]

[x, y, z]

[y, z]

The tail of [x, y, z] is [y, z].


Adding an element w to [x, y, z] doesnt change
[x, y, z] itself.

Lists, not arrays, are the fundamental data


structure in Functional Programming
Arrays are designed for random access; lists
are designed for working from one end
Complex data structures can be built more
easily from lists than from arrays

Recursion replaces loops as a fundamental


control structure
You never need to protect data with
mutexes (locks)
13

Standard higher-order
functions
A higher-order function is a function that (1) takes a function
as a parameter, or (2) returns a function as its value, or both
map(Fun, List)
Applies Fun to each element of List, returning a list of the results.
The result may contain values of a type different that those in List .

filter(Pred, List)
Returns a list of the elements of List that satisfy the predicate Pred.
The result will be a sublist of List .

The following standard function comes in a variety of flavors


whether it works left to right or right to left, and whether it
needs an explicit accumulator
reduce(Fun, Acc, List)
Calls Fun on successive pairs of elements of List , starting with Acc
The type returned is the type of Acc

14

You might also like