06, 2014
1 / 39
Outline
1
FP Introduction
Higher-order Functions
Immutability
Expression
Pattern Matching
Recursion
Lazy evaluation
Type Inference
Partial Function
Dr. Nguyen Hua Phung
2 / 39
Functional Programming
f(3)
f(x => x + 1)
parameter
Created dynamically
3+4
fg
4 / 39
Fundamental Theory
5 / 39
Mathematical Functions
A mathematical function is
a mapping of members of one set, called the domain
set, to another set, called the range set
6 / 39
Higher-order Functions
8 / 39
Function Composition
A function that
takes two functions as parameters and
yields a function whose value is the first actual
parameter function applied to the application of the
second
f g = f : (g : x)
For f(x) = x + 2; g(x) = x * x; f g = x * x + 2
Example in Scala,
val f
val g
val h
h(3)
val k
f(3)
= (x:Double) => x + 2
= (x:Double) => x * x
= f compose g
= f andThen g
9 / 39
Apply-to-all
A functional form that
takes a single function as a parameter and
yields a list of values obtained by applying the given
function to each element of a list of parameters
f :< x1 , x2 , ..., xn >=< f : x1 , f : x2 , ..., f : x2 >
For h(x)=x*x h:(1,2,3) yields (1,4,9)
Example in Scala,
List(2,3,4).map((x:Int) => x * x)
def inc (x:Int) = x + 1
List(4,5,6).map(inc)
10 / 39
Forall/Exist
A functional form that
takes a single predicate function as a parameter and
yields a value obtained by applying the given function
to each element of a list of parameters and take the
and/or of the results
T
f :< x1 , x2 , ..., xn >= S f : xi
f :< x1 , x2 , ..., xn >= f : xi
Example in Scala,
def isEqualToThree(x:Int) = x == 3
List(2,3,4).forall(isEqualToThree)
// yield false
List(2,3,4).exists(isEqualToThree)
// yield true
Dr. Nguyen Hua Phung
11 / 39
f
...
f
xn
x1
...
f
f
x0
f
x2
xn1
x1
xn
f
x0
12 / 39
Example in Scala
List(2,3,4).foldLeft(0)((a,b) => a+b) // yield 9
List(2,3,4).foldLeft(1)((a,b) => a*b) // yield 24
List(2,3,4).foldLeft("A")((a,b) => a + b)
// yield "A234"
List(2,3,4).foldRight("A")((a,b) => a + b)
// yield "234A"
13 / 39
Functions as Parameters
15 / 39
Closure [1]
16 / 39
17 / 39
Immutability
19 / 39
Example on Immutability
abstract class IntStack
def push(x: Int): IntStack =
new IntNonEmptyStack(x, this)
def isEmpty: Boolean
def top: Int
def pop: IntStack
class StackEmpty extends IntStack
def isEmpty = true
def top = error("EmptyStack.top")
def pop = error("EmptyStack.pop")
class IntNonEmptyStack(elem: Int, rest: IntStack)
extends IntStack
def isEmpty = false
def top = elem
def pop = rest
20 / 39
Everything is an expression
22 / 39
25 / 39
Recursion
27 / 39
29 / 39
Type Inference
31 / 39
32 / 39
33 / 39
Parameter Simplification
34 / 39
Partial Function
a partial function from X to Y (f: X 9 Y) is a function
f:
X Y, for some subset X of X.
:Z Z is a partial function.
Partial function in Scala is different from Partially
Applied Function concerned in Slide "Currying
Function"
Example in Scala,
type PF = PartialFunction[Int,String]
val one : PF = {case 1 => "one"}
one.isDefinedAt(1) //yield true
one.isDefinedAt(2) //yield false
val two: PF = {case 2 => "two"}
val oneOrTwo = one orElse two
val any: PF = {case _ => "any number"}
val num = one orElse two orElse any
Dr. Nguyen Hua Phung
36 / 39
Summary
38 / 39
References I
39 / 39