- UNIFEI - October - 2018
Prof. Edmilson
Hanneli Tavante
<3
Prolog
Prof. Maurilio
<3
Deutschland
Note: this list can change
WAT
Note: this list can change
[1, 2, 3, 4, 5]
No.
[1, 2, 3, 4, 5].map { |el| el*2 }
=> [2, 4, 6, 8, 10]
Which language is this??
A: Ruby!
[ el*2 for el in [1, 2, 3, 4, 5]]
[2, 4, 6, 8, 10]
Which language is this??
A: Python!
[ el*2 | el <- [1..5] ]
[2, 4, 6, 8, 10]
Which language is this??
A: Haskell!
List(1, 2, 3, 4, 5).map( el => el*2)
res0: List[Int] = List(2, 4, 6, 8, 10)
Which language is this??
A: Scala!
fn main() {
let res = vec![1, 2, 3, 4, 5].iter()
.map(|&el| el*2).collect::<Vec<_>>();
println!("{:?}", res);
}
[2, 4, 6, 8, 10]
Which language is this??
A: Rust!
Note: examples of statements: for, do/while, {...}, return
//pseudocode
int res = 0;
for (int i=0; i<= 100; i++) {
res = res + i;
}
What are the problems of this code?
Is there anything you can't predict?
There are two things to be aware:
res: 5050
sum[1..100]
5050
(This is Haskell)
sum[1..100]
sum()
1..100
Turing
Alonzo Church
"How can we formalize the concept of effective computability?"
Turing Machines
Lambda Calculus
J. McCarthy
Lisp Programming Language - one of the first functional languages
Functional Programming => One way to implement λ-calculus formalisms
Why is this programming style so important?
Powered by Mathematics!
Data
Operations
Type: a name for a collection of related values
{ True, False }: Bool
Let's ask Wikipedia:
"In programming languages, a type system is a collection of rules that assign a property called type to various constructs a computer program consists of, such as variables, expressions, functions or modules"
"Strong"
"Weak"
"Static"
"Dynamic"
* Definitions are not exact on literature
Java: static (why?)
Python: dynamic
"Static"
1. Reduce the number of runtime errors
2. The compiler can calculate the type of an expression
Type inference
3. The type the compiler calculates corresponds to the type you obtain when you execute the expression in runtime
Type Soundness
(group of elements, the types can be different)
(Sequence of values where all the elements have the same type)
This is the terminology we use to indicate that an expression has a certain type (::)
In Haskell,
Prelude> :type 1
1 :: Num t => t
Can you put 'Function' and 'type' in the same sentence so that you can come up with a definition of function?
"Function is a mapping of values from one type to another type"
Let's write a function to sum two integers in Haskell
Attempt #1
addd (x, y) = x+y
addd (3, 4)
7
Attempt #2
addd x y = x+y
addd 3 4
7
Attempt #1
addd (x, y) = x+y
addd (3, 4)
7
Attempt #2
addd x y = x+y
addd 3 4
7
:t addd
addd :: Num a => (a, a) -> a
:t addd
addd :: Num a => a -> a -> a
Attempt #2
addd x y = x+y
addd 3 4
7
:t addd
addd :: Num a => a -> a -> a
There is an advantage in writing the function like this:
It is a function (1)
That returns a function (2)
That takes another number (3)
And returns a number (4)
Attempt #2
addd x y = x+y
addd 3 4
7
:t addd
addd :: Num a => a -> a -> a
There is an advantage in writing the function like this:
:t addd
addd :: Num a => a -> (a -> a)
Is equivalent to:
Remember: Arrows associate to the right (*)
Attempt #2
addd x y = x+y
addd 3 4
7
:t addd
addd :: Num a => a -> (a -> a)
Now it is easier to see:
It is a function (1)
That returns a function (2)
That takes another number (3)
And returns a number (4)
:t addd
addd :: Num a => a -> a -> a
Remember: Arrows associate to the right
(*)
:t addd
addd :: Num a => a -> (a -> a)
Remember: Function applications associates to the left
mult x y z
((mult x) y) z
:t addd
addd :: Num a => a -> (a -> a)
addd 3.1 3.2
6.300000000000001
addd :: Int -> (Int -> Int)
addd x y = x+y
:t addd
addd :: Int -> Int -> Int
addd 3.1 3.2
error: No instance for
(Fractional Int)
arising from the literal ‘3.1’
[1, 2, 3, 4, 5]
What are lists in terms of expressions?
Whiteboard time! Let's understand the x:xs notation
What are lists in terms of expressions?
1:(2:(3:(4:(5:[ ]))))
(ex: transform elements into other elements, get only specific elements, transform lists into tuples)
A: List Comprehensions
[x^2 | x <- [1..5]]
generator:
defines how to generate the values of x
function to
transform x
[x | x <- [1..5], even x]
Get the number, no transformation
Get only the even numbers of a list
generator
filter
positions 2 [1, 2, 3, 4, 5, 2, 1, 3]
A: 1 and 5
Problem: Lists do not have an index!
positions
function name
x
xs
el
list
=
Strategy: transform each element of the list in a tuple: (el, pos)
[1, 2, 3, 4, 5, 2, 1, 3]
Our list
[0, 1, 2, 3, 4, 5, 6, 7]
Positions
Solution
(1, 0)
,(2, 1)
,(3, 2)
[
...
zip
operation
[0, 1, 2, 3, 4, 5, 6, 7]
Positions
Goal
[(1, 0), (2, 1) ...]
zip
operation
positions x xs =
zip xs [1, 2, 3, 4, 5, 2, 1, 3]
[0, 1, 2, 3, 4, 5, 6, 7]
Positions
Goal
[(1, 0), (2, 1) ...]
zip operation
positions x xs =
zip xs [1..n]
where n = length xs - 1
[0, 1, 2, 3, 4, 5, 6, 7]
Positions
Goal
[(1, 0), (2, 1) ...]
zip operation
positions x xs =
zip xs [1..n]
where n = length xs - 1
<-
( , )
el, pos
get pos
i |
[0, 1, 2, 3, 4, 5, 6, 7]
Positions
Goal
[(1, 0), (2, 1) ...]
zip operation
positions x xs =
zip xs [1..n]
where n = length xs - 1
<-
( , i)
el, pos
list comprehension
i |
[
Now we need to filter the elements that we specified
[0, 1, 2, 3, 4, 5, 6, 7]
Positions
Goal
[(1, 0), (2, 1) ...]
zip operation
positions x xs =
zip xs [1..n]
where n = length xs - 1
<-
( , i)
el, pos
list comprehension
i |
[
,
x'== x
(x', i)
]
What are functions?
Remember, Haskell has a strong link to mathematics
In λ-calculus, we have λ expressions
expression that denotes functions
λ:
Write the addd function as a λ expression
addd x y = x+y
One reason is being able to prove properties by mathematical induction :)
The practical reason here is that it helps us to get rid of statements
1. Let's define the types
length ::
[a]
-> Int
1. Let's define the types
length ::
[a]
-> Int
2. Define the base case
The "base case" is the one that will not change the result
length [] = 0
1. Let's define the types
length ::
[a]
-> Int
2. Define the base case
length [] = 0
3. Define the recursive structure
Add one to each element of the list, until you get an empty list
Remember: this is a list
1:(2:(3:(4:(5:[ ]))))
1. Let's define the types
length ::
[a]
-> Int
2. Define the base case
length [] = 0
3. Define the recursive structure
length ( : xs) = 1 + length xs
Anything
(_ : xs)
Whiteboard time
Use this video for reference (Computerphile)
IO/Monad (MOAR whiteboard)
How are expressions evaluated?
Lazy Evaluation
Church Numerals