Title: Functional Programming in
1Functional Programming in
Peter Achten - Rinus Plasmeijer University of
Nijmegen www.cs.kun.nl/clean
2Overview
- Predefined functions and operators on basic types
- Integer numbers
- Real numbers
- Booleans (truth values)
- Characters
- Predefined functions on
- a lists
- (a,b) tuples
3Predefined functions on lists
- hd a ? a
- hd abort "Cannot take the head of empty
list" - hd x _ x
- tl a ? a
- tl abort "Cannot take the tail of
empty list" - tl _ xs xs
-
4Take n-elements of a list
- Define a function that returns the first
n-elements of a list.If the list is smaller than
return just that list. - Name and type
- take Int a ? a
- 1. The list is empty
- take n
- 2. The list is not empty
- trivial case zero elements to look for
- take 0 list
- at least one element to look for
- take n e es .
5Take n-elements of a list
- Define a function that returns the first
n-elements of a list. - If the list is smaller than return just that
list. - Name and type
- take Int a ? a
- 1. The list is empty
- take n
- 2. The list is not empty
- trivial case zero elements to look for
- take 0 list
- at least one element to look for
- take n e es take (n-1) es
6Take n-elements of a list
- Define a function that returns the first
n-elements of a list. - If the list is smaller than return just that
list. - Name and type
- take Int a ? a
- 1. The list is empty
- take n
- 2. The list is not empty
- trivial case zero elements to look for
- take 0 list
- at least one element to look for
- take n e es e take (n-1) es
7Take n-elements of a list
- take Int a ? atake n take 0
list take n e es e take (n-1)
es - Start take 2 1, 2, 3
- Start // definition of Start
- ? take 2 1 2 3 // definition
of take, rule 3 - ? 1 take 1 2 3 // definition
of take, rule 3 - ? 1 2 take 0 3 // definition
of take, rule 2 - ? 1 2 // definition of take, rule
2 - 1,2
8Predefined functions on lists
- drop Int a ? a
- drop n drop 0 list list
- drop n e es drop (n-1) es
- Start ( take 2 1, 2, 3 , drop 2 1, 2,
3 ) -
- ? (1, 2, 3)
9List indexing
- (!!) infixl 9 a Int ? a // take n-th
element of list - (!!) x _ 0 x
- (!!) _ xs n
- ngt0 xs !! (n-1)
- (!!) _ n abort ("Cannot find element"
toString n "in list") - 1, 2, 3, 4, 5 !! 2 3
10Sum and Length on Integers
- sum Int ? Int // take sum of list
- sum 0
- sum x xs x sum xs
- length a ? Int // take length of list
- length 0
- length _ xs 1 length xs
-
11Sum, Length and Average on Integers
- sum Int ? Int // take sum of integer list
- sum 0
- sum x xs x sum xs
- length a ? Int // take length of any list
- length 0
- length _ xs 1 length xs
- -------
- average Int ? Int // take average of
integer list - average list sum list / length list
- Start average 1..10 5
-
12Sum, Length and Average on Reals
- sum Real ? Real // take sum of real list
- sum 0.0
- sum x xs x sum xs
- length a ? Int // take length of any list
- length 0
- length _ xs 1 length xs
- -------
- average Real ? Real // take average of
real list - average list sum list / toReal (length list)
- Start average 1.0..10.0 5.5
-
13Overloaded definitions of Sum and Average
- sum a ? a zero, a // take sum of list
- sum zero
- sum x xs x sum xs
- length a ? Int // take length of any list
- length 0
- length _ xs 1 length xs
- -------
- average a ? a , /, fromInt, zero a //
take average of list - average list sum list / fromInt (length list)
- Start ( average 1.0..10.0 5.5
- , average 1..10 5 )
-
14Append and Flatten
- 1. . 5 6 ..10 1,2,3,4,5,6,7,8,9,10
- () infixr 5 a a ? a // appending
lists - () list list
- () x xs list x xs list
- flatten 1. . 5 , 6 ..10
1,2,3,4,5,6,7,8,9,10 - flatten a ? a // flatten a list
- flatten
- flatten x xs x flatten xs
-
15Naïve Reverse
- reverse 1. . 5 5,4,3,2,1
- reverse a ? a // reverse a list
- reverse
- reverse x xs reverse xs x
-
16Naïve Reverse
- reverse reverse x xs
reverse xs x - () list list() x xs
list x xs list - reverse 1..5
- ? reverse 2..5 1 // definition of
reverse, rule 2 - ? (reverse 3..5 2) 1 //
definitions of reverse, rule 2 - ? (5 4) 3) 2) 1 //
definition of , rule 2 - ? (((5 4) 3) 2) 1 //
definition of , rule 1 - ? ((5,4 3) 2) 1 // definition
of , rule 2 - ? (5 4 3 2) 1 //
definition of , rule 2 - ? (5 4 3 2) 1 //
definition of , rule 1 - ? (5,4,3 2) 1 // and so on
- ? 5,4,3,2,1 // normal form reached
17Efficient Reverse with accumulator
- reverse a ? a // reverse a list
- reverse list reverse' list
- reverse' a a ? a // reverse with
accumulator - reverse' reversed_so_far
reversed_so_far - reverse' x xs reversed_so_far reverse'
xs x reversed_so_far - reverse 1..5 // definition of reverse
- ? reverse' 1..5 // definition of
reverse', rule 2 - ? reverse' 2..5 1 // definition of
reverse', rule 2 - ? reverse' 3..5 2 1 //
definition of reverse', rule 2 - ? reverse' 4..5 3..1 // definition of
reverse', rule 2 - ? reverse' 5 4..1 // definition of
reverse', rule 2 - ? reverse' 5..1 // definition of
reverse', rule 1 - ? 5..1 // normal form reached
18Local function definitions
- reverse a ? a // reverse a list
- reverse list reverse' list
- where
- reverse' a a ? a // reverse with
accumulator - reverse' reversed_so_far
reversed_so_far - reverse' x xs reversed_so_far reverse'
xs x reversed_so_far - After the 'where' one can define local function
definitions - They can only be used by the surrounding
functions - and the functions on the same level
- This is determined by the lay-out rule
19Local function definitions
- reverse a ? a // reverse a list
- reverse list reverse' list
- where
- reverse' a a ? a // reverse with
accumulator - reverse' reversed_so_far
reversed_so_far - reverse' x xs reversed_so_far reverse'
xs x reversed_so_far - The position of the first definition after a
where defines a border line - Each new definition on the same level must start
at this line - A previous definition continues if it starts to
the right of the line - A level is ended when a definition starts to the
left of the line -
20Local function definitions
- reverse a ? a // reverse a list
- reverse list reverse' list
- where
- reverse' a a ? a // reverse with
accumulator - reverse' reversed_so_far
- reversed_so_far
- reverse' x xs reversed_so_far
- reverse' xs x reversed_so_far
- The position of the first definition after a
where defines a border line. - Each new definition on the same level must start
at this line - A previous definition continues if it starts to
the right of the line - A level is ended when a definition start to the
left of the line -
21Local function definitions
- reverse a ? a // reverse a list
- reverse list reverse' list
- where
- reverse' a a ? a // reverse with
accumulator - reverse' reversed_so_far
reversed_so_far - reverse' x xs reversed_so_far reverse'
xs x reversed_so_far -
22Higher-order functions
- Higher-order functions are functions which have
a function as argument or as result. - at_zero (Int ?Int) ? Int
- at_zero f f 0
- inc n n 1
- square n n n
- ident n n
- at_zero inc ? inc 0 ? 0 1 ? 1
- at_zero square ? square 0 ? 0 0 ? 0
- at_zero ident ? ident 0 ? 0
23Higher-order functions
- Higher-order functions are functions which have
a function as argument or as result. - func_result Int ? (Int ? Int)
- func_result 0 inc
- func_result 1 square
- func_result f ident
- inc n n 1
- square n n n
- ident n n
- func_result 0 6 ? (func_result 0) 6 ? inc
6 ? 6 1 ? 7 - func_result 1 6 ? (func_result 1) 6 ? square 6
? 6 6 ? 36 - func_result 2 6 ? (func_result 2) 6 ? ident
6 ? 6
24Curried functions
- function application is left associative
- function x1 x2 xn
- ( ( ( function x1 ) x2 ) xn )
- function type is right associative
- function a1 a2 an?an1
- function (a1 ? (a2 ? ( ? (an?an1) )))
- plus Int Int ? Int plus (Int ? (Int ?
Int)) - plus x y x y plus x y (() x)
y
25Map a function over a list
- Define a function map which applies a function
to each argument of the list. - Name and type
- map (a ? b) a ? b
- 1. The list is empty
- map f
- 2. The list is not empty
- map f x xs f x map f xs
26Map a function over a list
- map (a ? b) a ? b
- map f
- map f x xs f x map f xs
- square Int ? Int
- square x x x
- Start map square 2, 3
- ? 4 , 9
- 2, 3 Int
- square (Int ? Int)
- map square 2, 3 Int
27Map a function over a list
- map (a ? b) a ? b
- map f
- map f x xs f x map f xs
- Start map square 2, 3
- Start // definition of Start
- ? map square 2 3 // definition of
map, rule 2 - ? square 2 map square 3 //
definition of square - ? 4 map square 3 // definition of
map, rule 2 - ? 4 square 3 map square //
definition of square - ? 4 9 map square // definition of
map, rule 1 - ? 4 9
- ? 4 , 9
28Map a function over a list
- map (a ? b) a ? b
- map f
- map f x xs f x map f xs
- square Int ? Int
- square x x x
- Start map (()1) 2, 3
- ? 3 , 4
- 2, 3 Int
- () (Int Int ? Int)
- (() 1) (Int ? Int)
29Map a function over a list
- map (a ? b) a ? b
- map f
- map f x xs f x map f xs
- Start map (()1) 2, 3
- Start // definition of Start
- ? map (()1) 2 3 // definition of
map, rule 2 - ? (()1) 2 map (()1) 3 //
definition of - ? 3 map (()1) 3 // definition of
map, rule 2 - ? 3 (()1) 3 map (()1) //
definition of - ? 3 4 map (()1) // definition of
map, rule 1 - ? 3 4
- ? 3 , 4