Unit 5-1
Unit 5-1
1. Introduction to FP
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
1.1 Principles of FP
treats computation as evaluation of mathematical
functions (and avoids state)
data and programs are represented in the same
way
functions as first-class values
– 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
it is a mathematical abstraction rather than a
programming language
1.2 History
lambda calculus (Church, 1932)
simply typed lambda calculus (Church, 1940)
lambda calculus as prog. lang. (McCarthy(?), 1960,
Landin 1965)
polymorphic types (Girard, Reynolds, early 70s)
algebraic types ( Burstall & Landin, 1969)
type inference (Hindley, 1969, Milner, mid 70s)
lazy evaluation (Wadsworth, early 70s)
Equational definitions Miranda 80s
Type classes Haskell 1990s
1.3 Varieties of FP languages
typed (ML, Haskell) vs untyped (Scheme,
Erlang)
Pure vs Impure
impure have state and imperative features
pure have no side effects, “referential
transparency”
Strict vs Lazy evaluation
1.4 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.
Logic programming – another paradigm
A program is viewed as a collection of logical rules
and facts (a knowledge-based system). Using logical
reasoning, the computer system can derive new facts
from existing ones.
1.5 Functional style of programming
A computing system is viewed as a function
which takes input and delivers output.
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.
Examples
Describing a game as a function
Text processing
Text processing: translation
Compiler
1.6 Why functional programming
Functional programming languages are carefully
designed to support problem solving.
There are many features in these languages which help
the user to design clear, concise, abstract, modular,
correct and reusable solutions to problems.
The functional Style of Programming allows the
formulation of solutions to problems to be as easy,
clear, and intuitive as possible.
Since any functional program is typically built by
combining well understood simpler functions, the
functional style naturally enforces modularity.
Why functional programming
Programs are easy to write because the system relieves the
user from dealing with many tedious implementation
considerations such as memory management, variable
declaration, etc .
Programs are concise (typically about 1/10 of the size of a
program in non-FPL)
Programs are easy to understand because functional
programs have nice mathematical properties (unlike imperative
programs) .
Functional programs are referentially transparent , that is, if a
variable is set to be a certain value in a program; this value
cannot be changed again. That is, there is no assignment but
only a true mathematical equality.
Why functional programming
Programs are easy to reason about because
functional programs are just mathematical
functions;
hence, we can prove or disprove claims about
our programs using familiar mathematical
methods and ordinary proof techniques (such as
those encountered in high school Algebra).
For example we can always replace the left
hand side of a function definition by the
corresponding right hand side.
1.7 Examples of FP languages
Lisp (1960, the first functional language….dinosaur, has no
type system)
Hope (1970s an equational fp language)
ML (1970s introduced Polymorphic typing systems)
Scheme (1975, static scoping)
Miranda (1980s equational definitions, polymorphic typing
Haskell (introduced in 1990, all the benefits of above +
facilities for programming in the large.)
Erlang (1995 - a general-purpose concurrent programming
language and runtime system, introduced by Ericsson)
The sequential subset of Erlang is a functional language, with
dynamic typing.
2. 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)
1. Arithmetic: +, -, *, /, ABS,
SQRT, REMAINDER, MIN, MAX
e.g., (+ 5 2) yields 7
Introduction to Scheme
2. QUOTE -takes one parameter; returns the
parameter without evaluation
QUOTE is required because the Scheme interpreter,
named EVAL, always evaluates parameters to
function applications before applying the function.
QUOTE is used to avoid parameter evaluation when
it is not appropriate
QUOTE can be abbreviated with the apostrophe
prefix operator
e.g., '(A B) is equivalent to (QUOTE (A B))
Introduction to Scheme
3. CAR takes a list parameter; returns the first
element of that list
e.g., (CAR '(A B C)) yields A
(CAR '((A B) C D)) yields (A B)
4. CDR takes a list parameter; returns the list
after removing its first element
e.g., (CDR '(A B C)) yields (B C)
(CDR '((A B) C D)) yields (C D)
Introduction to Scheme
5. CONS takes two parameters, the first of which
can be either an atom or a list and the second of
which is a list; returns a new list that includes
the first parameter as its first element and the
second parameter as the remainder of its result
e.g., (CONS 'A '(B C)) returns (A B C)
Introduction to Scheme
6. LIST - takes any number of parameters;
returns a list with the parameters as elements
Introduction to Scheme
Lambda Expressions
Form is based on notation
e.g., (LAMBDA (L) (CAR (CAR L)))
L is called a bound variable
Lambda expressions can be applied
e.g.,
((LAMBDA (L) (CAR (CAR L))) '((A B) C D))
Introduction to Scheme
A Function for Constructing Functions
DEFINE - Two forms:
1. To bind a symbol to an expression
e.g.,
(DEFINE pi 3.141593)
(DEFINE two_pi (* 2 pi))
Introduction to Scheme
2. To bind names to lambda expressions
e.g.,
(DEFINE (cube x) (* x x x))
Example use:
(cube 4)
Introduction to Scheme
Evaluation process (for normal functions):
1. Parameters are evaluated, in no particular order
2. The values of the parameters are substituted into
the function body
3. The function body is evaluated
4. The value of the last expression in the body is the
value of the function
(Special forms use a different evaluation process)
Introduction to Scheme
Example:
(DEFINE (square x) (* x x))
for every x A,
some element y=f(x) B
f(x) = run forever
terminate by raising an exception
1 3
2 2 2 2
3 3 1 3 1 1
Datatype Declarations
General form
datatype <name> = <clause> | … | <clause>
<clause> ::= <constructor> |<constructor> of <type>
Examples
datatype color = red | yellow | blue
Elements are red, yellow, blue
datatype atom = atm of string | nmbr of int
Elements are atm(“A”), atm(“B”), …, nmbr(0), nmbr(1), ...
datatype list = nil | cons of atom*list
Elements are nil, cons(atm(“A”), nil), …
cons(nmbr(2), cons(atm(“ugh”), nil)), ...
Datatypes and Pattern Matching
Recursively defined data structure
datatype tree = leaf of int | node of int*tree*tree
1 2 6 7
Recursive function
fun sum (leaf n) = n
| sum (node(n,t1,t2)) = n + sum(t1) + sum(t2)
Example: Evaluating Expressions
Define datatype of expressions
datatype exp = Var of int | Const of int | Plus of exp*exp;
Write (x+3)+y as Plus(Plus(Var(1),Const(3)), Var(2))
Evaluation function
fun ev(Var(n)) = Var(n)
| ev(Const(n)) = Const(n)
| ev(Plus(e1,e2)) = …
ev(Plus(Const(3),Const(2))) Const(5)
ev(Plus(Var(1),Plus(Const(2),Const(3))))
ev(Plus(Var(1), Const(5))
Case Expression
Datatype
datatype exp = Var of int | Const of int | Plus of
exp*exp;
Case expression
case e of
Var(n) => … |
Const(n) => …. |
Plus(e1,e2) => …
Evaluation by Cases
datatype exp = Var of int | Const of int | Plus of exp*exp;