13. ❏ no assignment statements
❏ no variables
WhatisFunctionalProgramming?
14. ❏ no assignment statements
❏ no variables
❏ once given a value, never change
WhatisFunctionalProgramming?
15. ❏ no assignment statements
❏ no variables
❏ once given a value, never change
❏ no side-effects at all
WhatisFunctionalProgramming?
16. ❏ no assignment statements
❏ no variables
❏ once given a value, never change
❏ no side-effects at all
WhatisFunctionalProgramming?
17. ❏ no assignment statements
❏ no variables
❏ once given a value, never change
❏ no side-effects at all
WhatisFunctionalProgramming?
18. ❏ no assignment statements
❏ no variables
❏ once given a value, never change
❏ no side-effects at all
WhatisFunctionalProgramming?
19. ❏ no assignment statements
❏ no variables
❏ once given a value, never change
❏ no side-effects at all
WhatisFunctionalProgramming?
20. WhatisFunctionalProgramming?
❏ no assignment statements
❏ no variables
❏ once given a value, never change
❏ no side-effects at all
“The functional programmer sounds
rather like a mediæval monk,
denying himself the pleasures of
life in the hope that it will make
him virtuous.”
24. WhyFunctionalProgrammingMatters?
❏ We strive for modularity
What procedural programming,
dependency injection, actor
model, microservices and
clean architecture have in
common?
33. Whatwillthisfunctiondo?
/**
* This function returns a reversed list
* @param list A list to be reversed
* @return A reversed list
*/
def reverse[T](list: List[T]): List[T] = ???
34.
35.
36. Whatwillthisfunctiondo?
/**
* This function returns a reversed list
* @param list A list to be reversed
* @return A reversed list
*/
def reverse[T](list: List[T]): List[T] = ???
37. Whatwillthisfunctiondo?
/**
* This function returns a reversed list
* @param list A list to be reversed
* @return A reversed list
def reverse[T](list: List[T]): List[T] = ???
116. HaskellHistoryinanutshell
❏ Miranda released 1985 - de facto FP ‘standard’
❏ At the FPCA'87 few formed a consensus that an open
standard should be defined for FP languages
❏ Haskell 1.0 defined in 1990
117. HaskellHistoryinanutshell
❏ Miranda released 1985 - de facto FP ‘standard’
❏ At the FPCA'87 few formed a consensus that an open
standard should be defined for FP languages
❏ Haskell 1.0 defined in 1990
❏ Haskell 98 in late 1997
118. HaskellHistoryinanutshell
❏ Miranda released 1985 - de facto FP ‘standard’
❏ At the FPCA'87 few formed a consensus that an open
standard should be defined for FP languages
❏ Haskell 1.0 defined in 1990
❏ Haskell 98 in late 1997
“(...) intended to specify a stable, minimal, portable
version of the language and an accompanying standard library
for teaching, and as a base for future extensions.”
119. HaskellHistoryinanutshell
❏ Miranda released 1985 - de facto FP ‘standard’
❏ At the FPCA'87 few formed a consensus that an open
standard should be defined for FP languages
❏ Haskell 1.0 defined in 1990
❏ Haskell 98 in late 1997
120. HaskellHistoryinanutshell
❏ Miranda released 1985 - de facto FP ‘standard’
❏ At the FPCA'87 few formed a consensus that an open
standard should be defined for FP languages
❏ Haskell 1.0 defined in 1990
❏ Haskell 98 in late 1997
❏ “The Haskell 98 Report” in 1999
121. HaskellHistoryinanutshell
❏ Miranda released 1985 - de facto FP ‘standard’
❏ At the FPCA'87 few formed a consensus that an open
standard should be defined for FP languages
❏ Haskell 1.0 defined in 1990
❏ Haskell 98 in late 1997
❏ “The Haskell 98 Report” in 1999
❏ “The Haskell 2010 Report” in 2009/2010
122. HaskellHistoryinanutshell
❏ Miranda released 1985 - de facto FP ‘standard’
❏ At the FPCA'87 few formed a consensus that an open
standard should be defined for FP languages
❏ Haskell 1.0 defined in 1990
❏ Haskell 98 in late 1997
❏ “The Haskell 98 Report” in 1999
❏ “The Haskell 2010 Report” in 2009/2010
❏ Further Haskell evolves rapidly around GHC
132. FunWithFunctions
❏ function without parameters is called definition
❏ function name must start with small letter
helloWorld = “Hello World”
133. FunWithFunctions
❏ function without parameters is called definition
❏ function name must start with small letter
> helloWorld
"Hello World"
it :: [Char]
helloWorld = “Hello World”
138. FunWithFunctions
> :t london
london :: [Char] -> [Char]
> london "baby"
"london baby"
it :: [Char]
london :: [Char] -> [Char]
london u = “London “ ++ u
167. Polymorphic types
> length [False, False, True]
3
> length [10, 20, 30, 40]
4
> length ["one", "two", "three"]
3
length :: [a] -> Int
‘a’ is a type variable, which means that length can be
applied to list of any type
189. List101
[2 ^ x | x <- [1..5]] -> [2,4,8,16,32]
take 7 [2 ^ x | x <- [1..]] -> [2,4,8,16,32,64,128]
190. List101
[2 ^ x | x <- [1..5]] -> [2,4,8,16,32]
take 7 [2 ^ x | x <- [1..]] -> [2,4,8,16,32,64,128]
[(x,y) | x <- [1..5], y <- [1..5]] -> [(1,1),(1,2),(1,3), ...
191. List101
[2 ^ x | x <- [1..5]] -> [2,4,8,16,32]
take 7 [2 ^ x | x <- [1..]] -> [2,4,8,16,32,64,128]
[(x,y) | x <- [1..5], y <- [1..5]] -> [(1,1),(1,2),(1,3), …
[(x,y) | x <- [1..2], y <- [x..2]] -> [(1,1),(1,2),(2,2)]
192. List101
[2 ^ x | x <- [1..5]] -> [2,4,8,16,32]
take 7 [2 ^ x | x <- [1..]] -> [2,4,8,16,32,64,128]
[(x,y) | x <- [1..5], y <- [1..5]] -> [(1,1),(1,2),(1,3), ...
[(x,y) | x <- [1..2], y <- [x..2]] -> [(1,1),(1,2),(2,2)]
[x | x <- [1..7], x `mod` 2 == 0] -> [2, 4, 6]
193. List101
[2 ^ x | x <- [1..5]] -> [2,4,8,16,32]
take 7 [2 ^ x | x <- [1..]] -> [2,4,8,16,32,64,128]
[(x,y) | x <- [1..5], y <- [1..5]] -> [(1,1),(1,2),(1,3), ...
[(x,y) | x <- [1..2], y <- [x..2]] -> [(1,1),(1,2),(2,2)]
[x | x <- [1..7], x `mod` 2 == 0] -> [2, 4, 6]
zip "abc" [1,2,3] -> [('a',1),('b',2),('c',3)]
194. List101
[2 ^ x | x <- [1..5]] -> [2,4,8,16,32]
take 7 [2 ^ x | x <- [1..]] -> [2,4,8,16,32,64,128]
[(x,y) | x <- [1..5], y <- [1..5]] -> [(1,1),(1,2),(1,3), ...
[(x,y) | x <- [1..2], y <- [x..2]] -> [(1,1),(1,2),(2,2)]
[x | x <- [1..7], x `mod` 2 == 0] -> [2, 4, 6]
zip "abc" [1,2,3] -> [('a',1),('b',2),('c',3)]
zip "abc" (cycle [0,1]) -> [('a',0),('b',1),('c',0)]
195. List101
[2 ^ x | x <- [1..5]] -> [2,4,8,16,32]
take 7 [2 ^ x | x <- [1..]] -> [2,4,8,16,32,64,128]
[(x,y) | x <- [1..5], y <- [1..5]] -> [(1,1),(1,2),(1,3), ...
[(x,y) | x <- [1..2], y <- [x..2]] -> [(1,1),(1,2),(2,2)]
[x | x <- [1..7], x `mod` 2 == 0] -> [2, 4, 6]
zip "abc" [1,2,3] -> [('a',1),('b',2),('c',3)]
zip "abc" (cycle [0,1]) -> [('a',0),('b',1),('c',0)]
zipWith (+) [1,2,3] [4,5,6] -> [5,7,9]
203. TypeClasses
❏ Eq
(==) :: a -> a -> Bool; (/=) :: a -> a -> Bool
❏ Ord
< , <= , > , >= :: a -> a -> Bool; min , max :: a -> a -> a
204. TypeClasses
❏ Eq
(==) :: a -> a -> Bool; (/=) :: a -> a -> Bool
❏ Ord
< , <= , > , >= :: a -> a -> Bool; min , max :: a -> a -> a
❏ Show
205. TypeClasses
❏ Eq
(==) :: a -> a -> Bool; (/=) :: a -> a -> Bool
❏ Ord
< , <= , > , >= :: a -> a -> Bool; min , max :: a -> a -> a
❏ Show
show :: a -> String
206. TypeClasses
❏ Eq
(==) :: a -> a -> Bool; (/=) :: a -> a -> Bool
❏ Ord
< , <= , > , >= :: a -> a -> Bool; min , max :: a -> a -> a
❏ Show
show :: a -> String
❏ Read
207. TypeClasses
❏ Eq
(==) :: a -> a -> Bool; (/=) :: a -> a -> Bool
❏ Ord
< , <= , > , >= :: a -> a -> Bool; min , max :: a -> a -> a
❏ Show
show :: a -> String
❏ Read
read :: String -> a
210. TypeClasses
> 10 == 20
False
> 10 < 20
True
> show 1 ++ " is a number"
"1 is a number"
211. TypeClasses
> 10 == 20
False
> 10 < 20
True
> show 1 ++ " is a number"
"1 is a number"
> read "5" + 10
15
212. TypeClasses
> 10 == 20
False
> 10 < 20
True
> show 1 ++ " is a number"
"1 is a number"
> read "5" + 10
15
> read "5"
<interactive>:85:1:
No instance for (Read a0) arising from a use of `read'
*Main> read "5" :: Integer 5 it :: Integer
213. TypeClasses
> 10 == 20
False
> 10 < 20
True
> show 1 ++ " is a number"
"1 is a number"
> read "5" + 10
15
> read "5" :: Integer
5
219. Typeclasses&classconstraints
add :: Num a => a -> a -> a
add a b = a + b
add 10 20
add 5.0 7.0
weirdo :: (Fractional t1, Num t) =>
t -> t1 -> [Char] -> (t, t1, [Char])
weirdo n d str = (n + 2, d / 2.0, str ++ "!!")
220. Typeclasses&classconstraints
add :: Num a => a -> a -> a
add a b = a + b
add 10 20
add 5.0 7.0
weirdo :: (Fractional t1, Num t) =>
t -> t1 -> [Char] -> (t, t1, [Char])
weirdo n d str = (n + 2, d / 2.0, str ++ "!!")
weirdo 7 7.0 “kuku”
221. Typeclasses&classconstraints
add :: Num a => a -> a -> a
add a b = a + b
add 10 20
add 5.0 7.0
weirdo :: (Fractional t1, Num t) =>
t -> t1 -> [Char] -> (t, t1, [Char])
weirdo n d str = (n + 2, d / 2.0, str ++ "!!")
weirdo 7 7.0 “kuku”
weirdo 7.0 5.0 “mono”
231. FunWithFunctions,Part2
first :: (a, b, c) -> a
first (x, _, _) = x
second :: (a, b, c) -> b
second (_, y, _) = y
third :: (a, b, c) -> c
third (_, _, z) = z
250. Creatingnewtypes-Datadeclarations
data OS = Windows | OSX | Linux | Unix
bootup :: OS -> [Char]
bootup Unix = "Up and running"
bootup Linux = "Well, I was actually never down..."
251. Creatingnewtypes-Datadeclarations
data OS = Windows | OSX | Linux | Unix
bootup :: OS -> [Char]
bootup Unix = "Up and running"
bootup Linux = "Well, I was actually never down..."
bootup OSX = "Non-sufficient Starbucks coffee, please refill"
252. Creatingnewtypes-Datadeclarations
data OS = Windows | OSX | Linux | Unix
bootup :: OS -> [Char]
bootup Unix = "Up and running"
bootup Linux = "Well, I was actually never down..."
bootup OSX = "Non-sufficient Starbucks coffee, please refill"
bootup Windows = "Upgrading to Windows 10, this should take a
second.."
255. Creatingnewtypes-Datadeclarations
data OS = Windows | OSX | Linux | Unix
class Bootable a where
boot :: a -> [Char]
instance Bootable OS where
boot Unix = "Up and running"
boot Linux = "Well, I was actually never down..."
boot OSX = "Non-sufficient Starbucks coffee, please refill"
boot Windows = "Upgrading to Windows 10, this should take a
second.."
257. Creatingnewtypes-Datadeclarations
data OS = Windows | OSX | Linux | Unix
instance Eq OS where
Unix == Unix = True
Linux == Linux = True
OSX == OSX = True
Windows == Windows = True
_ == _ = False
258. Creatingnewtypes-Datadeclarations
data OS = Windows | OSX | Linux | Unix
instance Eq OS where
Unix == Unix = True
Linux == Linux = True
OSX == OSX = True
Windows == Windows = True
_ == _ = False
> Unix == Windows
False
260. Creatingnewtypes-Datadeclarations
data OS = Windows | OSX | Linux | Unix
instance Show OS where
show Unix = "Unix"
show Linux = "Linux"
show OSX = "OSX"
show Windows = "Windows"
261. Creatingnewtypes-Datadeclarations
data OS = Windows | OSX | Linux | Unix
instance Show OS where
show Unix = "Unix"
show Linux = "Linux"
show OSX = "OSX"
show Windows = "Windows"
> show Unix
"Unix"
265. Creatingnewtypes-Datadeclarations
data OS = Windows | OSX | Linux | Unix
deriving (Eq, Ord, Show, Read)
> Windows == Unix
False
> show Windows
"Windows"
> read "Windows"::OS
Windows
> Linux > Windows
True
266. Creatingnewtypes-Datadeclarations
data Shape = Circle Float | Rect Float Float
deriving Show
> Circle 10
Circle 10.0
data Option a = Nothing | Some a
deriving Show
> Some “one”
Some “one”
267. Creatingnewtypes-Datadeclarations
data Shape = Circle Float | Rect Float Float
deriving Show
> Circle 10
Circle 10.0
data Option a = Nothing | Some a
deriving Show
> Some “one”
Some “one”
286. FizzBuzz
fizzes = cycle ["", "", "fizz"]
buzzes = cycle ["", "", "", "", "buzz"]
pattern = zipWith (++) fizzes buzzes
fizzbuzz = zipWith combine pattern [1..]
where combine word number =
if null word then show number else word
287. FizzBuzz
fizzes = cycle ["", "", "fizz"]
buzzes = cycle ["", "", "", "", "buzz"]
pattern = zipWith (++) fizzes buzzes
fizzbuzz = zipWith combine pattern [1..]
where combine word number =
> take 16 pattern if null word then show number else word
["1","2","fizz","4","buzz","fizz","7","8","fizz","buzz","
11","fizz","13","14","fizzbuzz","16"]
291. Whatnext?
1. Read “Why Functional Programming Matters paper”
2. Try to implement examples from that paper using Haskell
292. Whatnext?
1. Read “Why Functional Programming Matters paper”
2. Try to implement examples from that paper using Haskell
3. Sign for FP101x
293. Whatnext?
1. Read “Why Functional Programming Matters paper”
2. Try to implement examples from that paper using Haskell
3. Sign for FP101x
https://courses.edx.org/courses/course-v1:
DelftX+FP101x+3T2015/courseware/79633017711e44c9878df4f33701
4766/
294. Whatnext?
1. Read “Why Functional Programming Matters paper”
2. Try to implement examples from that paper using Haskell
3. Sign for FP101x
4. Try out Frege (Haskell on the JVM)
295. Whatnext?
1. Read “Why Functional Programming Matters paper”
2. Try to implement examples from that paper using Haskell
3. Sign for FP101x
4. Try out Frege (Haskell on the JVM)
https://github.com/Frege/frege