Functional Programming - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

Functional Programming

Description:

It is formed by sewing a quilt to the right of another quilt of equal height ... All occurrences of x in E2 are said to be within the scope of this binding ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 51
Provided by: Wir5
Category:

less

Transcript and Presenter's Notes

Title: Functional Programming


1
Functional Programming
  • Element of Functional Programming

2
Functional 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
3
Functional 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
4
Functional 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
5
A LITTLE LANGUAGE OF EXPRESSIONS
  • Basic Values
  • Constants Names for Basic Values
  • Operations
  • Expressions
  • Convenient Extensions
  • Local Declarations

6
A 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.

7
Example Quilt
  • What is Quilt?

8
Little Quilt
  • Little Quilt manipulates geometric objects with
  • Height
  • Width
  • Texture

9
Basic 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

10
Operations
  • Quilt can be turned and can be sewn together

11
Rules
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

12
Constants 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
13
Expressions
  • 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)
14
Expressions
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)
15
Convenient 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)))
16
Local 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
17
User-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))
19
Review 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

20
Specification 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
21
Summary 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

22
TYPEsValues 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.

23
The syntax of type expressions
lttype-expgt lttype-namegt
lttype-expgt ? lttype-expgt lttype-expgt
lttype-expgt lttype-expgt list
24
Structured 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.

25
Operations 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

26
Operations for Constructing and Inspecting Values
  • Basic Types
  • Operations on Basic Values
  • Products of Types
  • Operations on Pairs
  • List of Elements
  • Operations on Lists

27
Basic 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
28
Operations 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

29
Operations 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
30
Products 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
31
Products 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
32
Operations 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
33
Lists 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
34
Lists 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
35
Operations 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
36
Operations on Lists
  • For Example

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
37
Functions 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
38
Functions 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
39
Function 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.

40
APPOACHES TO EXPRESSION EVALUATION
  • The rules for expression are base on the
    structure of expressions

E1 E2
  1. Evaluate the subexpressions E1 and
  2. Evaluate the subexpressions E2 and
  3. Add two values

41
Innermost 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
42
Selection 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
43
Evaluation 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
44
Othermost Evaluation
  • Substitute the actual for the formal in the
    function body
  • Evaluate the body
  • Return its value as the answer.

45
Example Function
Fun f(x) if x gt 100 then x-10 else
f(f(x11))
46
Innermost 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

47
Outermost 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

48
LEXICAL 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
49
Val 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
50
Fun 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
Write a Comment
User Comments (0)
About PowerShow.com