FP c02 NoAnim
FP c02 NoAnim
FP c02 NoAnim
Definition
A letter followed by letters, digits, , ’
Example
long_name_from_FP
x’’
a_b_3
Remark
The length of the name is arbitrary.
Basics: Constants
pi = 3.14
Definition
Type = collection of values + functions over them
Type inference = calculate an expression’s type at compile time
Integer type in ML
I int : arbitrary precision
I binary operations: + - * div mod
I relational operators: <, >, <=, >=, =, <>
Primitive types: Real
Example
3.14
-10.0
1.0E-2
1.4142135 or 1.4142135623730951
Basic types: Real
Real type in ML
I real
I operations: + - * /
I relational operators: <, >, <=, >=
I relational operators <> and = are implemented using
<, >, <=, >=
Example
3.14
˜10.0
1.0E˜2
Primitive types: Boolean
Boolean type in ML
I type: bool; values: true, false
I logical operations: not, andalso, orelse
I conditional expression: if E then ET else EF
Character
Example (Haskell:Char)
’a’,’b’,...,’z’,’3’
’\n’,’\\’
Example (ML)
char type in ML is based on strings of length 1.
#"a" introduces a char in ML.
String
Definition (String)
Sequence of 0 or more characters between double quotes (").
"abc","a1b2",""
Operators
1. relational:
I Haskell: <, >, <=, >=, ==, /=
I ML: <, >, <=, >=, =, <>
2. concatenation:
I Haskell: ++
I ML: ˆ
Enumerative type
Definition
It is meant to express alternative data.
pi3 = 3.14
area::Double -> Double
area r = pi3 * r * r
Example
> fun sq x = x * x;
Example
Recursion
It is used to express repetitions in FP
Remark
gcd has only one recursive call in its body. It is linear recursive.
Recursive functions
Fibonacci numbers
F0 = 0, F1 = 1, Fn = Fn−1 + Fn−2
Example (Fibonacci)
Remark
fib has more than one recursive calls in its body.
Recursive functions
Remark
The number of recursive calls of this function increases
extremely rapidly with its arguments.
A little bit of lists and functions
Definition (List)
A list is a sequence of values of a specific type.
Example (Haskell)
Example (ML)
> [1,2,3];
val it = [1, 2, 3] : int list
A little bit of lists and functions
Observations
1. the length of the list is not limited
2. the types of list elements are not restricted
3. however, the elements must have the same type
A little bit of lists and functions
Definition (Function)
A function is a mapping from values of one type to values of
some other type.
Prelude> :t not
> not;
Calling functions
FP: f x (y*z) (g t)
Note
1. as a rule, no parentheses separate the function name and
arguments; however, in ML we might have this separation
2. pay attention to the precedence: fact n - 1 versus
fact (n-1); if not sure, use parentheses
A little bit of lists and functions
Example
1. Prelude> head [1,2,3]
1
2. Prelude> tail [1,2,3]
[2,3]
3. Prelude> length [1,2,3]
3
4. Prelude> [1,2,3] ++ [4,5]
[1,2,3,4,5]
5. Prelude> 1:[2,3]
[1,2,3]
6. Prelude> [1,2,3] !! 1
2
A little bit of lists and functions
Example
1. > hd [1,2,3];
val it = 1: int
2. > tl [1,2,3];
val it = [2, 3]: int list
3. > length [1,2,3];
val it = 3: int
4. > [1,2,3] @ [4,5];
val it = [1, 2, 3, 4, 5]: int list
5. > 1::[2,3];
val it = [1, 2, 3]: int list
6. > List.nth ([1,2,3],1);
val it = 2: int
Tuples
Definition
A tuple is an anonymous constructor containing at least two
components; component order matters.
Observation
But: this is just a shortcut for a pair of real numbers.
Tuples
Example
data Gender = M | F
Main> location
"L.A." :: [Char]
Main> gender
ERROR - Cannot find "show" function for:
*** Expression : gender
*** Of type : Gender
Printing components of a tuple
datatype gender = m | f;
val n = #1 a1;
> n;
val it = "Brad" : string
Records in ML
Components of a record
A record is composed of a number of fields. Each field has a
name, called tag. Field order does not matter.
> #l a1;
val it = "L.A." : string
Recursion: express repetition in FP
Definition
A function is tail recursive if it returns either something
computed directly or something returned by its recursive call
(the last thing it does is to call itself)
Example
Example
Example
fact(3) =
3*fact(3-1) = 3*fact(2) =
3*(2*fact(2-1)) = 3*(2*fact(1)) =
3*(2*(1*fact(1-1))) = 3*(2*(1*fact(0))) =
3*(2*1) = 3*2 =
6
factacc: no space and time wasted
Example
factacc(3,1) =
factacc(3-1,3*1) = factacc(2,3) =
factacc(2-1,2*3) = factacc(1,6) =
factacc(1-1,1*6) = factacc(0,6) =
6
Tail recursive functions are important
Reasons
1. we can see a tail recursive function as being equivalent to
an iteration
2. a tail recursive function could automatically be re-written in
an iterative manner
3. writing tail recursive functions helps saving space (of the
stack) and time (for stacking and un-stacking values)
Local declarations: Haskell
f x = let
doubleof z = 2 * z
ten = 10
(a,b) = x
in
doubleof(ten * (a + b))
Prelude> f(2,3)
100
Local declarations: Haskell
qs [] = []
qs (x:xs) = qs smalls ++ [x] ++ qs bigs
where
smalls = [s | s <- xs, s<=x]
bigs = [b | b <- xs, b>x]
Notes
1. don’t forget that in Haskell indentation is important.
2. Local declarations enter the scope in the same time. Their
types are infered by the system.
Local declarations: ML
local
fun fibit n p c =
if n=0 then c
else fibit (n-1) c (p+c)
in
fun fib n = fibit n 1 0
end;
Note
fibit is a private function of fib
Local declarations: ML
Note
All values on the right hand side are evaluated and after the
identifiers on the left hand side get the corresponding values.
Operators
Definition
An infix operator is a function written between its arguments.
The reason for doing this is syntactic sugaring.
Example
Note
In ML, infix, infixr mean left and right associativity
respectively. We can also specify the operator’s precedence
(e.g. in the example below, the operator’s precedence is 6).
Example
Example
Note
In Haskell, infix, infixl and infixr mean free, left and
right associativity respectively.
Example
infixl 6 ##
Note
An infix operator could be used as prefix function.
3 ‘div‘ 2 same as
div 3 2
Mutually Recursive Functions
A mathematical series
π 1 1 1 1 1
4 =1− 3 + 5 − 7 + ··· + 4k+1 − 4k+3 + ...
pos 1 = 1
pos 5 = 1 − 13 + 15
pos 9 = 1 − 13 + 15 − 17 + 19
neg 3 = 1 − 13
neg 7 = 1 − 13 + 15 − 17
neg 11 = 1 − 13 + 15 − 17 + 19 − 11
1
Mutually Recursive Functions
Example
Example
n = 10; i = 1; s = 0;
sum: if i<=n then s = s+i; goto sum1
else stop
sum1: i = i+1;
goto sum