0% found this document useful (0 votes)
15 views22 pages

Programming Language:: Design and Implementation

Functional programming focuses on values and functions rather than state and memory, allowing for a more declarative approach to computation. Key characteristics include first-class functions, recursion, lazy evaluation, and referential transparency, which enhances mathematical reasoning. Pure functional languages, like Haskell, lack imperative features, while impure languages, like LISP, may include them.

Uploaded by

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

Programming Language:: Design and Implementation

Functional programming focuses on values and functions rather than state and memory, allowing for a more declarative approach to computation. Key characteristics include first-class functions, recursion, lazy evaluation, and referential transparency, which enhances mathematical reasoning. Pure functional languages, like Haskell, lack imperative features, while impure languages, like LISP, may include them.

Uploaded by

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

Programming Language:

Design and Implementation

Functional Programming

Session 3
Functional Programming
In contrast to procedural languages, functional programs don't
concern themselves with state and memory locations. Instead, they
work exclusively with values, and expressions and functions which
compute values.
- Functional programming is not tied to the von Neumann machine.
- It is not necessary to know anything about the underlying hardware
when writing a functional program, the way you do when writing an
imperative program.
- Functional programs are more declarative than procedural ones; i.e.
they describe what is to be computed rather than how it should be
computed.
Functional Languages
Common characteristics of functional programming languages:

1. Simple and concise syntax and semantics.


2. Repetition is expressed as recursion rather than
iteration.
3. Functions are first class objects. I.e. functions can be
manipulated just as easily as integers, floats, etc. in
other languages.
4. Data as functions. I.e. we can build a function on the
fly and then execute it. (Some languages).
Functional Languages...
5. Higher-order functions. I.e. functions can take
6. functions as arguments and return functions as results.
Lazy evaluation. Expressions are evaluated only when
needed. This allows us to build infinite data structures,
where only the parts we need are actually constructed.
(Some languages).
7. Garbage Collection. Dynamic memory that is no longer
needed is automatically reclaimed by the system. GC is
also available in some imperative languages (Modula-
3, Eiffel) but not in others (C, C++, Pascal).
Functional Languages...
8. Polymorphic types. Functions can work on data of different
types. (Some languages).
9. Functional programs can be more easily manipulated
mathematically than procedural programs.

Pure vs. Impure FPL


 Some functional languages are pure, i.e. they contain no imperative
features at all. Examples: Haskell, Miranda, Gofer.

 Impure languages may have assignment-statements, goto:s, while-


loops, etc. Examples: LISP, ML, Scheme.
What is a function?
 A function maps argument values (inputs) to result values (outputs).

 A function takes argument values from a source set (or domain).

 A function produces result values that lie in a target set (or range).
More on functions
 A function must not map an input value to more than one output
value. Example:
More on functions...
 If a function F maps every element in the domain to some element
in the range, then F is total. I.e. a total function is defined for all
arguments.
More on functions...
 A function that is undefined for some inputs, is called partial.

 Divide is partial since & =? is undefined.


Specifying functions
A function can be specified extensionally or intentionally.
Extensionally:
 Enumerate the elements of the (often infinite) set of pairs
"(argument, result)" or "Argument Result."
 The extensional view emphasizes the external behavior (or
specification), i.e. what the function does, rather than how it does it.

Double= {… , (1,2), (5,10), }


Even= {… , (0, True), (1, False), …}
Double= {… , 1 2, 5 10, ...}
isHandsome= {Chris True, Hugh False}
Specifying functions
Intensionally:
 Give a rule (i.e. algorithm) that computes the result from the
arguments.
 The intentional view emphasizes the process (or algorithm) that is
used to compute the result from the arguments.

double x = 2 * x
even x = x mod 2 == 0
isHandsome x = if isBald x
then True
else False
Specifying functions…
Graphically:
 The graphical view is a notational variant of the
intentional view.
Function Application
 The most important operation in a functional program is function
application, i.e. applying an input argument to the function, and
retrieving the result:

double x = 2 * x
even x = x mod 2 == 0

double 5 10
even 6 True
Function Composition
Function composition makes the result of one function
application the input to another application:

double x = 2 * x
even x = x mod 2 == 0

even (double 5) even 10 True


Function Definition - Example
Example: How many numbers are there between m and n, inclusive?
Extensional Definition:
sumbetween m n = {… (1,1) 1, (1, 2) 2, … , (2, 10) 9}
Intentional Definition:
sumbetween m n = ((m + n) * (abs (m-n) + 1)) div 2
Graphical Definition:
Function Signatures
To define a function we must specify the types of the input and output
sets (domain and range, i.e. the function's signature), and an algorithm
that maps inputs to outputs.
Referential Transparency
 The most important concept of functional programming is
referential transparency. Consider the expression
(2*3)+5*(2*3)
(2*3) occurs twice in the expression, but it has the same meaning
(6) both times.
 RT means that the value of a particular expression (or sub-
expression) is always the same, regardless of where it occurs.
 This concept occurs naturally in mathematics, but is broken by
imperative programming languages.

 RT makes functional programs easier to reason about mathematically.


Referential Transparency
Evaluate even (double 5) :

double x = 2 * x
even x = x mod 2 == 0

even (double 5)
even (2 * 5)
even 10
10 mod 2 == 0
0 == 0 True
Referential Transparency
So, isn't Pascal referentially transparent??? Well, sometimes, yes, but not
always. If a Pascal function ƒ has side-effects (updating a global variable,
doing input or output), then ƒ(3) + ƒ(3) may not be the same as 2* ƒ(3).
I.e. The second ƒ(3) has a different meaning than the first one.
var G: integer;
Function f (n:integer) : integer;
begin G:= G+1; f:=G+n; end;

begin begin
G:= 0; G:= 0;
print f(3) +f(3); print 2*f(3);
{print 4+5=9} {print 2*4=8}
end end
Referential Transparency
Furthermore, in many imperative languages the order in which the
arguments to a binary operator are evaluated are undefined.

var G: integer;
Function f (n:integer) : integer;
begin G:= G+1; f:=G+n; end;

Left (3) evaluated first. right f(3) evaluated first.


Begin Begin
G:= 0; G:= 0;
Print f(3) – f(4); Print f(3) – f(4);
Print 4 – 6= – 2 Print 5 – 5= 0
end. end.
Referential Transparency
This cannot happen in a pure functional language.

1. Expressions and sub-expressions always have the same value,


regardless of the environment in which they're evaluated.
2. The order in which sub-expressions are evaluated doesn't effect the
final result.
3. Functions have no side-effects.
4. There are no global variables.
5. Variables are similar to variables in mathematics: they hold a value,
but they can't be updated.
Referential Transparency
6. Variables aren't (updatable) containers the way they are imperative
languages.
7. Hence, functional languages are much more like mathematics than
imperative languages. Functional programs can be treated as
mathematical text, and manipulated using common algebraic laws.

You might also like