50
Functional Programming Element of Functional Programming

Functional Programming Element of Functional Programming

Embed Size (px)

Citation preview

Page 1: Functional Programming Element of Functional Programming

Functional Programming

Element of Functional Programming

Page 2: Functional Programming Element of Functional Programming

Functional Programming

Functional Programming began as computing with expressions.

The following are examples:

2 An integer constantX A variablelog n Function log applied to n2+3 Function + applied to 2 and 3

Page 3: Functional Programming Element of Functional Programming

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 > y) then x else y

Page 4: Functional Programming Element of Functional Programming

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

Page 5: Functional Programming Element of Functional Programming

A LITTLE LANGUAGE OF EXPRESSIONS

Basic Values Constants: Names for Basic Values Operations Expressions Convenient Extensions Local Declarations

Page 6: Functional Programming Element of Functional Programming

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.

Page 7: Functional Programming Element of Functional Programming

Example : Quilt

What is Quilt?

Page 8: Functional Programming Element of Functional Programming

Little Quilt

Little Quilt manipulates geometric objects with

Height Width Texture

Page 9: Functional Programming Element of Functional Programming

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

Page 10: Functional Programming Element of Functional Programming

Operations

Quilt can be turned and can be sewn together

turn turn turn turn

sew

Page 11: Functional Programming Element of Functional Programming

Rules

1. A quilt is one of the primitive pieces, or

2. It is formed by turning a guilt clockwise 90, or

3. It is formed by sewing a quilt to the right of another quilt of equal height

4. Nothing else is a quilt

Quilts and the operations on them are specified by the following rules:

Page 12: Functional Programming Element of Functional Programming

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

Name “a”

Name “b”

Operation called• turn• sew

Page 13: Functional Programming Element of Functional Programming

Expressions

The syntax of expressions mirrors the definition of quilts:

Complex expressions are built up from simpler ones,

<exp> ::= a | b | turn(<exp>) | sew(<exp>,<exp>)

Page 14: Functional Programming Element of Functional Programming

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)

Page 15: Functional Programming Element of Functional Programming

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 inFun unturn(x) = turn(turn(turn(x)))Fun pile(x,y) = unturn(sew(turn(y),turn(x)))

Page 16: Functional Programming Element of Functional Programming

Local Declarations

Let-expressions or let-bindings allow declarations to appear within expressions.

Let-expression FormLet <declaration> in <expression> end

For examplelet fun unturn(x) = turn(turn(turn(x))) fun pile(x,y) = unturn(sew(turn(y),turn(x)))in pile(unturn(b),turn(b))end

Page 17: Functional Programming Element of Functional Programming

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 <name> = <expression>

Page 18: Functional Programming Element of Functional Programming

Value declarations are used together with let-bindings.

An expression of the formlet val x=E1 in E2 end

let val bnw = unturn(b)in pile(bnw,turn(b))end

RewritePile(unturn(b),turn(b))

Page 19: Functional Programming Element of Functional Programming

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

Page 20: Functional Programming Element of Functional Programming

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

Page 21: Functional Programming Element of Functional Programming

Summary of Little Quilt

<exp> ::= a | b <exp> ::= turn(<exp>) | sew(<exp>,<exp>)<exp> ::= let <declarations> in <exp> end<decs> ::= <dec> | <dec> <decs><dec> ::= fun <name> (<formals>) = <exp><formals> ::= <name> | <name>, <formals><exp> ::= <name> (<actuals>)<actuals> ::= <exp> | <exp>, <actuals><dec> ::= val <name> = <exp><exp> ::= <name>

Page 22: Functional Programming Element of Functional Programming

TYPEs:Values 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.

Page 23: Functional Programming Element of Functional Programming

The syntax of type expressions

<type-exp> ::= <type-name> | <type-exp> <type-exp> | <type-exp> *<type-exp> | <type-exp> list

Page 24: Functional Programming Element of Functional Programming

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.

Page 25: Functional Programming Element of Functional Programming

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

Page 26: Functional Programming Element of Functional Programming

Operations for Constructing and Inspecting Values

Basic Types Operations on Basic Values Products of Types Operations on Pairs List of Elements Operations on Lists

Page 27: Functional Programming Element of Functional Programming

Basic Types

A type is basic if its values are atomic If the values are treated as whole elements,

with no internal structure.

For exampleThe boolean values in the set {true,false}

Page 28: Functional Programming Element of Functional Programming

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

Page 29: Functional Programming Element of Functional Programming

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

int*int bool

Page 30: Functional Programming Element of Functional Programming

Products of Types

The product A*B of two types A and B consists of ordered pairs written as

For Example(1,“one”) is a pair consisting of int:1 and string: “one”

(a,b) ; where a is a value of type A and b is a value of type B

Page 31: Functional Programming Element of Functional Programming

Products of Types

A product of n types A*A*A*…*A consists of tuples written as(a1, a2 , a3 ,…, an ) ; where ai is a value of type A1

Page 32: Functional Programming Element of Functional Programming

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) = xfun second(x,y) = y

Page 33: Functional Programming Element of Functional Programming

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

Page 34: Functional Programming Element of Functional Programming

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

Page 35: Functional Programming Element of Functional Programming

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 listhd(x) The first or head element of list x.tl(x) The tail or rest of the list.a::x Construct a list with head a and tail x

Page 36: Functional Programming Element of Functional Programming

Operations on Lists

For ExampleGiven x is [1, 2, 3] Null(x) false Hd(x) 1 Tl(x) [2, 3]From the following equalities: [1, 2, 3] = 1::[2, 3] = 1::2::[3] = 1::2::3::[]

Page 37: Functional Programming Element of Functional Programming

Functions from a Domain to a Range

The type A B is the set of all functions from A to B

For ExampleIf 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 Q*Q Q

Page 38: Functional Programming Element of Functional Programming

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 AA 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

Page 39: Functional Programming Element of Functional Programming

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.

Page 40: Functional Programming Element of Functional Programming

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 and3. Add two values

Page 41: Functional Programming Element of Functional Programming

Innermost Evaluation

Evaluate the expression represented by <actual-parameter>

Substitute the result for the formal in the function body

Evaluate the body Return its value as the answer

<name> <actual-parameter>

Page 42: Functional Programming Element of Functional Programming

Selection Evaluate

<condition> is an expression that evaluates to either true or false.

True : <expression1> is evaluated False : <expression2> is evaluated

If <condition> then <expression1> else <expression2>

Page 43: Functional Programming Element of Functional Programming

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

Page 44: Functional Programming Element of Functional Programming

Othermost Evaluation

Substitute the actual for the formal in the function body

Evaluate the body Return its value as the answer.

Page 45: Functional Programming Element of Functional Programming

Example Function

Fun f(x) = if x > 100 then x-10 else f(f(x+11))

Page 46: Functional Programming Element of Functional Programming

Innermost Evaluate

F(100) = if 100>100 then 100-10 else f(f(100+11)) = f(f(100+11)) = f(f(111)) = f( if 111> 100 then 111-10 else f(f(111+11)) ) = f(111-10) = f(101) = if 101>100 then 101-10 else f(f(101+11)) = 101 – 10 = 91

Page 47: Functional Programming Element of Functional Programming

Outermost Evaluate

F(100) = if 100>100 then 100-10 else f(f(100+11))

= f(f(100+11))

= if f(111> 100) then

f(111+11)-10 else f(f(f(100+11)+11)) )

F(100+11) = if 100+11> 100 then

100+11-10 else f(f(100+11+11))

= if 111>100 then

100+11-10 else f(f(100+11+11))

= 100+11-10 = 111-10 = 101

Page 48: Functional Programming Element of Functional Programming

LEXICAL SCOPE

Renaming is made precide by introducing a notion of local or “bound” variables;bound occurrences of variables can be renamed without changing the meaning of a program

fun successor(x) = x + 1fun successor(n) = n + 1

Page 49: Functional Programming Element of Functional Programming

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 x=E1 in E2 end

let val x=2 in x+x end

Page 50: Functional Programming Element of Functional Programming

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)=x+1 in 2*f(x) end