Title: CS 2104 : Prog. Lang. Concepts. Functional Programming I
1CS 2104 Prog. Lang. Concepts.Functional
Programming I
- Lecturer Dr. Abhik Roychoudhury
- School of Computing
- From Dr. Khoo Siau Chengs lecture notes
2Outline
- What is Functional Programming?
- Basic Expressions
- Using Higher-order Functions
- List Processing
- Value and Type Constructions
3Features of Functional Programming
- Expressions The main construct in the language
- Functions First-class citizens of the language
- Types for specification and verification
- List Processing
4Computation
- Achieved via function application
- Functions are mathematical functions without
side-effects. - Output is solely dependent of input.
5Outline
- What is Functional Programming?
- Basic Expressions
- Using Higher-order Functions
- List Processing
- Value and Type Constructions
6Basic SML Expressions
ltExpgt ltConstantsgt ltIdentifiersgt ltExp1gt
ltopgt ltExp2gt if ltExp0gt then ltExp1gt else
ltExp2gt let ltDeclgt in ltExpgt end fn
ltPatterngt ltPatterngt gt ltExpgt ltExp1gt
ltExp2gt (ltExp1gt, ltExp2gt , ltExp3gt)
ltList-Expgt ltConstantsgt all
constants ltIdentifiersgt all identifiers ltopgt
all binary operators
7If-expression, Not If-statement
if lte0gt then lte1gt else lte2gt
if (3 gt 0) then 4x else x-4
4x
If-expression evaluates to a value.
8Constructing a Linear List
ltList-Expgt ltExpgt , ltExpgt nil
ltExpgt ltExpgt
nil 1nil 1(2nil) (x2)(y-3)(x3)
nil
Note A list must end with nil.
9Constructing a Linear List
ltList-Expgt ltExpgt , ltExpgt nil
ltExpgt ltExpgt
nil 1nil 1(2nil) (x2)(y-3)(x3)
nil
Operator is right associative.
head
1 1,2 x2,y-3,x3
10Constructing a Tuple
(ltExpgt , ltExpgt , ltExpgt)
Tuple is like a record without field names.
(1,2) (1,2,True,4,False) (x2,y3,x3)
11Giving An Expression A Name
Declaring an expression with a name enables easy
reference.
ltdeclgt val ltpatterngt ltexpgt
Identifiers are legally declared after proper
pattern matching.
val x 3 val (y,z) (x3,x-3) val a,b
x2,y-3
val a,b x2
val (xxs) 1,2,3,4,5
12Pattern-Match a List
13Local Declaration/Definition
let ltdeclgt in ltexpgt end
let val x (3,4) val (a,b) x in a a b
end
15
Identifiers are statically scoped
14Nested bindings of same var.
- Let val x 2 in let val x x 1 in xx end end
- Let val x 2 in let val y x 1 in y y end
end - Let val x 2 in (x1)(x1) end
- 9
15Function Declaration and Function Application
ltDeclgt fun ltvargt ltparagt ltexpgt ltparagt
ltpatterngt ltpatterngt
fun fac x if (x gt 0) then x (fac
(x-1)) else 1
fac 2 ? if (2 gt 0) then 2 (fac (2-1)) else 1 ?
2 (fac 1) ? 2 (if (1 gt 0) then 1 (fac
(1-1)) else 1) ? 2 (1 (fac 0)) ? 2 (1
1) ? 2
16Function as Equations
A function can be defined by a set of equations.
ltDeclgt fun ltvargt ltparagt ltexpgt
ltvargt ltparagt ltexpgt
fun fac 0 1 fac n n (fac (n-1))
Function application is accomplished via pattern
matching.
fac 2 ? 2 (fac (2-1)) ? 2 (fac 1) ? 2
(1 (fac (1-1))) ? 2 (1 (fac 0)) ? 2 (1
1) ? 2
17Outline
- What is Functional Programming?
- Basic Expressions
- Using Higher-order Functions
- List Processing
- Value and Type Constructions
18Functions as First-Class Citizens
- Lambda abstraction A nameless function
- fn ltparagt gt ltExpgt
(fn (x,y) gt x y) (2,3) ? 23 ? 5
Definition Application val newfn
fn (x,y) gt x y newfn(2,4) newfn(4,3)
19Functions as First-Class Citizens
- Passing function as argument
- fun apply (f, x) f x
apply (fac, 3) ? fac 3 ? 6
apply (fn x gt x2, 3) ? (fn x gt x2) 3 ?
32 ? 6
20Functions as First-Class Citizens
- Returning function as result
- fun choose (x,f,g) if x then f else g
choose(a b,fn x gt x2, fac)(4) returns
either 8 or 24
fun compose (f,g) fn x gt f (g x)
(compose (add3,fac)) 4 ? (fn x gt add3 (fac
x))(4) ? (add3 (fac 4)) ? 27
compose (f,g) is denoted as f o g
21Functions as First-Class Citizens
- Storing function in data structure
val flist fac, fib, add3 let val
f1,f2,f3 flist in (f1 2) (f2 3) (f3
4) end
22Functions as Algorithms
- A function describes a way for computation.
- Self-Recursive function
- fun factorial (x) if (x lt 1) then 1
- else x factorial (x-1)
- Mutual-Recursive functions
- fun even(0) true
- even(1) false
- even(x) odd(x-1)
- and odd(x) even(x-1)
23Passing Arguments to a Function
How many argument does this function really take?
fun f (x,y) x 2 y
Calling f with argument (1,2) yields 5.
fun g x y x 2 y
Calling g with arguments 1 and 2 yields 5.
24Outline
- What is Functional Programming?
- Basic Expressions
- Using Higher-order Functions
- List Processing
- Value and Type Constructions
25Exploring a List
fun length 0 length (xxs) 1
length(xs)
length(10,10,10) gt 1 length(10,10)
gt 1 1 length(10) gt 1 1 1
length() gt 1 1 1 0 gt 3
Note how a list is being consumed from head to
tail.
26fun append(,ys) ys append(xxs,ys)
x append(xs,ys)
append(1,2,3) gt 1append(2,3) gt 1
2append(,3) gt 123 gt 1,2,3
Note how a new list is being produced from head
to tail.
append(x,y) is denoted by x _at_ y.
27fun rev(xs) let fun r(,ys) ys
r(xxs,ys) r(xs,xys) in r(xs,)end
rev(1,2,3) gt r(1,2,3,) gt
r(2,3,1) gt r(3,21) gt
r(,321) gt 3,2,1
Note how lists are being produced and consumed
concurrently.
28Map operation
map square 1,2,3,4 gt square 1, square
2,.., square 4 gt 1,4,9,16
fun map f map f (xxs) (f x)
(map f xs)
map square 1,2 gt (square 1) (map square
2) gt 1 (square 2) (map square ) gt
1 4 1,4
29Map operation
fun map f map f (xxs) (f x)
(map f xs)
Law for Map
map f (map g list) map (f o g) list
map f (map g a1,..,an) map f g a1,..,g an
f (g a1),.., f (g an) (f o g) a1,.., (f o g)
an map (f o g) a1,..an
30Reduce Operation
reduce ? a1,..,an a0 a1 ? (a2 ? (.. ? (an
? a0))) reduce f a1,..,an a0 f(a1,f(a2,f(..,
f(an,a0))))
31reduce (op ) 2,4,6 1 gt 2 (4 (6
1)) gt 48
reduce (fn (x,y)gt1y) 2,4,6 0 gt 1 (1
(1 0)) gt 3
32Outline
- What is Functional Programming?
- Basic Expressions
- Using Higher-order Functions
- List Processing
- Value and Type Constructions
33Types Classification of Values and Their
Operators
Basic Types
Type Values Operations bool true,false ,
ltgt, int ,1,0,1,2, ,ltgt,lt,,div, real ..
,0.0,.,3.14,.. ,ltgt,lt,,/, string foo,\q\
, ,ltgt,
Boolean Operations e1 andalso e2 e1
orelse e2
34Types in ML
- Every expression used in a program must be
well-typed. - It is typable by the ML Type system.
- Declaring a type
- 3 int
- 1,2 int list
- Usually, there is no need to declare the type in
your program ML infers it for you.
35Structured Types
Structured Types consist of structured values.
- Structured values are built up through
- expressions. Eg (23, square 3)
- Structured types are denoted by type
- expressions.
lttype-exprgt lttype-namegt lttype-constantgt
lttype-exprgt lttype-exprgt
lttype-exprgt ? lttype-exprgt lttype-exprgt list
36Type of a Tuple
(1,2) int int (3.14159, x3,true) real
int bool
A B set of ordered pairs (a,b)
Data Constructor ( , ) as in (a,b) Type
Constructor as in A B
In general, (a1,a2,,an) belongs to A1A2An.
37Type of A List
Type Constructor list
1,2,3 int list 3.14, 2.414 real
list 1, true, 3.14 ??
Not well-typed!!
A list set of all lists of A -typed values.
A in A-list refers to any types (intint)
list , (1,3), (3,3),(2,1), int list
list , 1,2, 1,0,1,2,2,3,
38Function TypesDeclaring domain co-domain
fac int -gt int
A -gt B set of all functions from A to B.
Type Constructor -gt Data Construction via
1. Function declaration fun f x x 1 2.
Lambda abstraction fn x gt x 1
Value Selection via function application f 3
? 4 (fn x gt x 1) 3 ? 4
39Sum of Types Enumerated Types
datatype Days Mo Tu We Th Fr Sa
Su
New Type
data / data constructors
Selecting a summand via pattern matching case d
of Sa gt Go to cinema Su gt Extra
Curriculum _ gt Life goes on
40Combining Sum and Product of Types Algebraic
Data Types
Defining an integer binary tree datatype IntTree
Leaf int Node of (IntTree, int, IntTree)
fun height (Leaf x) 0 height
(Node(t1,n,t2)) 1 max(height(t1),height(t2))
41Conclusion
- A functional program consists of an expression,
not a sequence of statements. - Higher-order functions are first-class citizen in
the language. - It can be nameless
- List processing is convenient and expressive
- In ML, every expression must be well-typed.
- Algebraic data types empowers the language.