0% found this document useful (0 votes)
113 views

Functional Programming

- Functional programming uses expressions rather than statements and avoids mutable data and side effects. Functions are treated as first-class values that can be passed as arguments to other functions. - Higher-order functions take other functions as arguments. Purely functional programs operate on immutable data and have referential transparency. - Features of functional programming languages include functions as values, no variables/assignments, recursion instead of loops, and independence from evaluation order. Converting imperative programs to functional style can be difficult due to lack of mutable structures and first-class functions.

Uploaded by

gautham_atluri
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

Functional Programming

- Functional programming uses expressions rather than statements and avoids mutable data and side effects. Functions are treated as first-class values that can be passed as arguments to other functions. - Higher-order functions take other functions as arguments. Purely functional programs operate on immutable data and have referential transparency. - Features of functional programming languages include functions as values, no variables/assignments, recursion instead of loops, and independence from evaluation order. Converting imperative programs to functional style can be difficult due to lack of mutable structures and first-class functions.

Uploaded by

gautham_atluri
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Functional Programming

In functional programming, programs are executed by evaluating expressions Functional programming requires that functions are first-class
which means that they are treated like any other values and can be passed as arguments to other functions or be returned as a result of a function. Being first-class also means that it is possible to define and manipulate functions from within other functions.

Higher-order functions HOFs are functions that take other functions as their arguments. Higher-order functions are very useful for refactoring code and reduce the amount of repetition.

Purity Some functional languages allow expressions to yield actions in addition to return values. These actions are called side effects to emphasize that the return value is the most important outcome of a function Languages that prohibit side effects are called pure. Immutable data Purely functional programmes typically operate on immutable data. Instead of altering existing values, altered copies are created and the original is preserved. Since the unchanged parts of the structure cannot be modified, they can often be shared between the old and new copies, which saves memory.

Referential transparency Pure computations yield the same value each time they are invoked. This property is called referential transparency and makes possible to conduct equational reasoning on the code. For instance if y = f x and g = h y y then g can be written as : g = h (f x) (f x)

Lazy evaluation Since pure computations are referentially transparent they can be performed at any time and still yield the same result. This makes it possible to defer the computation of values until they are needed, that is, to compute them lazily. Lazy evaluation avoids unnecessary computations and allows, for example, infinite data structures to be defined and used.

Recursion
Recursion is the only way to iterate. A recursive function is tail recursive if the final result of the recursive call is the final result of the function itself. Tail recursive call optimisation ensure that heavy recursion does not consume excessive memory. If the result of the recursive call must be further processed it is not tail recursive.

Benefits of functional programming


Provides better support for structured programming than imperative programming. Easy to create clean and simple abstractions. For instance, to abstract out a recurring piece of code by creating a higher-order function, which will make the resulting code more declarative and comprehensible.

Functional programs are often shorter and easier to understand

Features of functional programming languages


All procedures are functions and clearly distinguish incoming values from outgoing values. There are no variables or assignments variables are replaced by parameters. There are no loops- loops are replaced by recursive calls. The value of a function depends only on the values of its parameters and not on the order of evaluation or the execution path that led to the call. Functions are first-class values.

Why is it difficult to translate all the imperative programs to functional style?


Structured values such as arrays and records cannot be returned values from functions There is no way to build a value of a structured type directly. Functions are not first-class values, so higher order functions cannot be written.

Imperative version
void gcd ( int u, int v, int* x) { int y, t, z; z=u; y=v; While (y!=0) { t=y; y=z%y; z=t; } *x=z; }

Functional version

int gcd (int u, int v) { if (v == 0) return u; else return gcd(v, u%v); }

Imperative version

Functional version

int sum( int i, int j) { int k, temp; temp = 0; for (k = I; k<=j; k++) temp +=k; return temp; }

int sum ( int i, int j) { if( i > j ) return 0; else return (i + sum ( i + 1, j)); }

Converting functions into tail recursive using accumulating parameters.


Accumulating parameters are used to precompute operations performed after the recursive call, and pass the results to the recursive call.
int sum1 ( int i, int j, int sumsofar) { helping procedure if( i > j) return sumsofar; else return sum1 ( i + 1, j, sumsofar + i ); }; int sum ( int i, int j) { return sum1 (i, j, 0); }

accumulating parameter

Scheme: A Dialect of Lisp


All programs and data in Scheme are expressions . Expressions are of two varieties: Atoms constants and identifiers, includes numbers, strings, names, functions Lists a sequence of expressions separated by spaces and surrounded by parentheses

Examples of Scheme expressions


42 hello #T #\a (2.1 2.2 3.1) a hello (+ 2 3 ) - a number a string the Boolean value true the character a a list of numbers - an identifier an identifier - a list consisting of the identifier + and two numbers (* (+ 2 3 ) (/ 6 2) a list consisting of an identifier followed by 2 lists Case insensitive: aBc and ABC are the same atom

Evaluation rule for expressions


Constant atoms, such as numbers and strings, evaluate to themselves. Identifiers are looked up in the current environment an replaced by the values found there. A list is evaluated by recursively evaluating each element in the list as an expression; the first expression in the list must evaluate a function. This function is then applied to the evaluated values of the rest of the list. applicative order evaluation

Atoms: a, 7, tom, my-age, nil, T

Evaluate an atom gives the value assigned that atom. Numbers are special--they always evaluate to themselves. 7 => 7 my-age => Error (variable not defined)
Assign a value to an atom with setq and/or setf (setq my-age 24) => 24 my-age => 24 Reserved: nil and T nil => nil t => t

Lists: (a), (+ 6 7), (a (e f g) h), (), nil


nil is both an atom and a list. It is equivalent to the empty list: (). Evaluating a list always invokes a function. (function-name arg1 argn) (+ 6 7) => 13 (foo 17 18 19) => Error (function not defined) (+ my-age 4) => 28 When evaluating a list, first evaluate all the arguments and then apply the function to all those results.

Some expressions in C and Scheme


3+4*5 (a == b) && ( a! = 0) gcd ( 10, 35) gcd getchar()

(+3( *4 5)) (and ( = a b ) (not ( = a 0 ))) (gcd 10 35) gcd (read- char)

The quote function


Purpose is to prevent evaluation > (2.1 2.2 2.3) Error : the object 2.1 is not a procedure > (quote (2.1 2.2 2.3)) (2.1 2.2 2.3) The short-hand notation for it the single quote > `(2.1 2.2 2.3) (2.1 2.2 2.3)

If elsif construct
(if ( = a 0 ) 0 (/ 1 a )) (cond (( = a 0) 0) (( = a 1) 1) (else (/ 1 a)))
Note: ; stands for comment

; if a=0 then return 0 ; else return 1/a ; if a=0 then return 0 ; elsif a=1 then return 1 ;else return 1/a

You might also like