Title: Functional Programming
1Functional Programming
- Element of Functional Programming
2Functional Programming
- Functional Programming began as computing with
expressions. - The following are examples
2 An integer constant X A
variable log n Function log applied to n 23
Function applied to 2 and 3
3Functional Programming
- Expressions can also include conditionals and
function definitions.
Example The value of following condition
expression is the maximum of x and y if (x gt y)
then x else y
4Functional Programming
- For concreteness, specific ML examples will be
written in a typewriter-like font - For example
- Computing with expressions will be introduced by
designing a little language of expressions.
2 2 val it 4 int
5A LITTLE LANGUAGE OF EXPRESSIONS
- Basic Values
- Constants Names for Basic Values
- Operations
- Expressions
- Convenient Extensions
- Local Declarations
6A LITTLE LANGUAGE OF EXPRESSIONS
- Expressions are formed by giving names to values
and to operations on them - The little language manipulates just one type of
value geometric objects called quilts.
7Example Quilt
8Little Quilt
- Little Quilt manipulates geometric objects with
9Basic Values
- Given two primitive objects in the language are
the square pieces - Each quilt has
- A fixed direction or orientation
- A height
- A width
- A texture
10Operations
- Quilt can be turned and can be sewn together
11Rules
Quilts and the operations on them are specified
by the following rules
- A quilt is one of the primitive pieces, or
- It is formed by turning a guilt clockwise 90, or
- It is formed by sewing a quilt to the right of
another quilt of equal height - Nothing else is a quilt
12Constants Names for Basic Values
- The first step in construction a language to
specify quilts is to give names to the primitive
pieces and to the operations on quilts
- Operation called
- turn
- sew
Name a
Name b
13Expressions
- The syntax of expressions mirrors the definition
of quilts - Complex expressions are built up from simpler
ones,
ltexpgt a b turn(ltexpgt)
sew(ltexpgt,ltexpgt)
14Expressions
Sew(turn(turn(b)),a)
No expression quilt 1 b 2
turn(b) 3 turn(turn(b)) 4
a 5 sew(turn(turn(b)),a)
15Convenient Extensions
- Expressions will now be extended by allowing
functions from quilts to quilts. - It would be convenient to give names to the
operations. - Once defined, functions like unturn and pile can
used as if they were built in
Fun unturn(x) turn(turn(turn(x))) Fun pile(x,y)
unturn(sew(turn(y),turn(x)))
16Local Declarations
- Let-expressions or let-bindings allow
declarations to appear within expressions. - Let-expression Form
Let ltdeclarationgt in ltexpressiongt end
For example let fun unturn(x)
turn(turn(turn(x))) fun pile(x,y)
unturn(sew(turn(y),turn(x))) in
pile(unturn(b),turn(b)) end
17User-Defined Names for Values
- The final extension is convenient for writing
large expressions in terms of simpler ones. - A value declaration form
- Gives a name to a value
val ltnamegt ltexpressiongt
18- Value declarations are used together with
let-bindings. - An expression of the form
let val xE1 in E2 end
let val bnw unturn(b) in
pile(bnw,turn(b)) end
Rewrite Pile(unturn(b),turn(b))
19Review Design of Little Quilt
- The language Little Quilt was defined by starting
with values and operations. - The values are quilts, built up from two square
pieces - The operations are for turning and sewing quilts.
- The language began with the name a and b for the
square pieces and - The names turn and sew for the operations
20Specification of a quilt
Let fun unturn(x) turn(turn(turn(x))) fun
pile(x,y) unturn(sew(turn(y),turn(x)))
val aa pile(a,turn(turn(a))) val bb
pile(unturn(b),turn(b)) val p sew(bb,aa)
val q sew(aa,bb) In pile(p,q) end
21Summary of Little Quilt
- ltexpgt a b
- ltexpgt turn(ltexpgt) sew(ltexpgt,ltexpgt)
- ltexpgt let ltdeclarationsgt in ltexpgt end
- ltdecsgt ltdecgt ltdecgt ltdecsgt
- ltdecgt fun ltnamegt (ltformalsgt) ltexpgt
- ltformalsgt ltnamegt ltnamegt, ltformalsgt
- ltexpgt ltnamegt (ltactualsgt)
- ltactualsgt ltexpgt ltexpgt, ltactualsgt
- ltdecgt val ltnamegt ltexpgt
- ltexpgt ltnamegt
22TYPEsValues and Operations
- A types consists of a set of elements called
values together with a set of functions called
operations. - We will consider methods for defining structured
values such as products, lists, and functions.
23The syntax of type expressions
lttype-expgt lttype-namegt
lttype-expgt ? lttype-expgt lttype-expgt
lttype-expgt lttype-expgt list
24Structured value
- Structured values such as lists can be used as
freely in functional languages as basic values
like integers and strings - Value in a function language take advantage of
the underlying machine, but are not tied to it.
25Operations for Constructing and Inspecting Values
- The structuring methods will be presented by
specifying a set of values together with a set of
operations. - Concentrate on operations for constructing and
inspecting the elements of the set. - For example
- To Extend a list by adding a new first element
- To test whether a list is empty
26Operations for Constructing and Inspecting Values
- Basic Types
- Operations on Basic Values
- Products of Types
- Operations on Pairs
- List of Elements
- Operations on Lists
27Basic Types
- A type is basic if its values are atomic
- If the values are treated as whole elements, with
no internal structure. - For example
The boolean values in the set true,false
28Operations on Basic Values
- Basic values have no internal structure, so the
only operation defined for all basic types is a
comparison for equality - For example
- The equality 2 2 is true,
- The inequality 2!2 is false
29Operations on Basic Values
- Technically, an operation is a function
- The equality test on integers is a function from
pairs of integers to boolean.
The type intint ? bool
30Products of Types
- The product AB of two types A and B consists of
ordered pairs written as - For Example
- (1,one) is a pair consisting of int1 and
string one
(a,b) where a is a value of type A and
b is a value of type B
31Products of Types
- A product of n types AAAA consists of tuples
written as
(a1, a2 , a3 ,, an ) where ai is a value of
type A1
32Operations on Pairs
- A pair is constructed from a and b by writing
(a,b) - Associated with pairs are operations called
projection functions to extract the first and
second elements from a pair. - The first element of the pair (a,b) is a
- The second element is b
- Projection functions can be readily defined
fun first(x,y) x fun second(x,y) y
33Lists of Elements
- A list is a finite-length sequence of elements.
- For Example
- int list consists of all lists of
integers.
The type A list consists of all lists of
elements, where each element belongs to type A
34Lists of Elements
- List elements will be written between brackets
and - List elements will be separated by commas
- The empty list is written equivalently as
- For Example
1, 2, 3 is a list of
three integers red, white, blue is a
list of three strings
35Operations on Lists
- Lists-manipulation programs must be prepared to
construct and inspect lists of any length. - The following operations on list are from ML
null(x) True is x is the empty list hd(x) The
first or head element of list x. tl(x) The tail
or rest of the list. ax Construct a list with
head a and tail x
36Operations on Lists
Given x is 1, 2, 3 Null(x) false Hd(x) 1
Tl(x) 2, 3 From the following equalities
1, 2, 3 12, 3 123 123
37Functions from a Domain to a Range
- The type A ? B is the set of all functions from A
to B - For Example
If Q is the set of quilt then function turn
from quilts to quilts is Q ? Q function sew
form pairs of quits to quilts is QQ ? Q
38Functions from a Domain to a Range
- A function f in A ?B is total
- A function f in A ?B is partial
If it associates an element of B with each
element of A A is called the domain and B is
called the range of f.
It is possible for there to be no element of B
associated with an element of A
39Function Application
- The set A ? B is application which takes a
function f in A? B and an element a in A and
yields and element b of B. - For Example
- A function is applied in ML f a is the
application of f to a.
40APPOACHES TO EXPRESSION EVALUATION
- The rules for expression are base on the
structure of expressions
E1 E2
- Evaluate the subexpressions E1 and
- Evaluate the subexpressions E2 and
- Add two values
41Innermost Evaluation
- Evaluate the expression represented by
ltactual-parametergt - Substitute the result for the formal in the
function body - Evaluate the body
- Return its value as the answer
ltnamegt ltactual-parametergt
42Selection Evaluate
- ltconditiongt is an expression that evaluates to
either true or false. - True ltexpression1gt is evaluated
- False ltexpression2gt is evaluated
If ltconditiongt then ltexpression1gt
else ltexpression2gt
43Evaluation of Recursive Functions
- The actual parameters are evaluated and
substituted into the function body.
Length(X) ((cond (null X) 0 ( 1 (length(cdrX)))))
Length(hello, world) 1
length(world)
1 1 length()
1 1 0
2
44Othermost Evaluation
- Substitute the actual for the formal in the
function body - Evaluate the body
- Return its value as the answer.
45Example Function
Fun f(x) if x gt 100 then x-10 else
f(f(x11))
46Innermost Evaluate
- F(100) if 100gt100 then 100-10 else f(f(10011))
- f(f(10011))
- f(f(111))
- f( if 111gt 100 then 111-10 else
f(f(11111)) ) - f(111-10)
- f(101)
- if 101gt100 then 101-10 else
f(f(10111)) - 101 10 91
47Outermost Evaluate
- F(100) if 100gt100 then 100-10 else f(f(10011))
- f(f(10011))
- if f(111gt 100) then
- f(11111)-10 else
f(f(f(10011)11)) ) - F(10011) if 10011gt 100 then
- 10011-10 else
f(f(1001111)) - if 111gt100 then
- 10011-10 else
f(f(1001111)) - 10011-10 111-10 101
48LEXICAL SCOPE
- Renaming is made precide by introducing a notion
of local or bound variablesbound occurrences
of variables can be renamed without changing the
meaning of a program
fun successor(x) x 1 fun successor(n) n 1
49Val Bindings
- Binding occurrence or simply binding of x
- All occurrences of x in E2 are said to be within
the scope of this binding - Occurrences of x in E1 are not in the scope of
this binding of x
let val xE1 in E2 end
let val x2 in xx end
50Fun Bindings
- This binding of the formal parameter x is visible
only to the occurrences of x in E1 - This binding of the function name f is visible to
all occurrences of f in both E1 and E2
let fun f(x)E1 in E2 end
let fun f(x)x1 in 2f(x) end