Documente Academic
Documente Profesional
Documente Cultură
Ana Iova
Denisa Diaconescu
1
Currying
Currying
2
Funct, iile curry si uncurry s, i mult, imi
Exemplu:
f : : ( I n t , S t r i n g ) −> S t r i n g
f ( n , s ) = take n s
not
• Dacă notăm B → C = {h : B → C | h funct, ie}
observăm că fx ∈ B → C pentru orice x ∈ A .
4
Quiz time!
Seria 23: https://questionpro.com/t/AT4qgZvHuO
5
Agregarea elementelor dintr-o listă - fold
Exemplu - Suma
Solut, ie recursivă
sum : : [ I n t ] −> I n t
sum [ ] = 0
sum ( x : xs ) = x + sum xs
6
Exemplu - Produs
Solut, ie recursivă
product : : [ I n t ] −> I n t
product [ ] = 1
product ( x : xs ) = x * product xs
7
Exemplu - Concatenare
Solut, ie recursivă
concat : : [ [ a ] ] −> [ a ]
concat [ ] = []
concat ( xs : xss ) = xs ++ concat xss
Prelude > concat [ " con " , " ca " , " t e " , " na " , " r e " ]
" concatenare "
8
Funct, ia foldr
9
Funct, ia foldr
Solut, ie recursivă
f o l d r : : ( a −> b −> b ) −> b −> [ a ] −> b
foldr f i [ ] = i
f o l d r f i ( x : xs ) = f x ( f o l d r f i xs )
10
Exemplu — Suma
Solut, ie recursivă
sum : : [ I n t ] −> I n t
sum [ ] = 0
sum ( x : xs ) = x + sum xs
Exemplu
f o l d r ( + ) 0 [ 1 , 2 , 3 ] == 1 + ( 2 + ( 3 + 0 ) )
11
Exemplu — Produs
Solut, ie recursivă
product : : [ I n t ] −> I n t
product [ ] = 1
product ( x : xs ) = x * product xs
Exemplu
f o l d r ( * ) 1 [ 1 , 2 , 3 ] == 1 * ( 2 * ( 3 * 1 ) )
12
Exemplu — Concatenare
Solut, ie recursivă
concat : : [ [ a ] ] −> [ a ]
concat [ ] = []
concat ( xs : xss ) = xs ++ concat xss
Exemplu
f o l d r ( + + ) [ ] [ " Ana " , " are " , " mere . " ]
== " Ana " ++ ( " are " ++ ( " mere . " ++ [ ] ) )
13
Exemplu – Suma pătratelor numerelor pozitive
f : : [ I n t ] −> I n t
f xs = sum ( squares ( p o s i t i v e s xs ) )
f : : [ I n t ] −> I n t
f xs = sum [ x * x | x <− xs , x > 0 ]
f : : [ I n t ] −> I n t
f [] = 0
f ( x : xs ) | x > 0 = ( x * x ) + f xs
| otherwise = f xs
14
Exemplu – Suma pătratelor numerelor pozitive
f : : [ I n t ] −> I n t
f xs = f o l d r ( + ) 0 (map s q r ( f i l t e r pos xs ) )
where
sqr x = x * x
pos x = x > 0
f : : [ I n t ] −> I n t
f xs = f o l d r ( + ) 0
(map (\ x -> x * x)
( f i l t e r (\ x -> x > 0) xs ) )
f : : [ I n t ] −> I n t
f xs = f o l d r ( + ) 0 (map ( ˆ 2) ( f i l t e r ( > 0) xs ) )
f : : [ I n t ] −> I n t
f = f o l d r ( + ) 0 . map ( ^ 2 ) . f i l t e r ( >0)
15
Exemplu - Compunerea funct, iilor
16
Quiz time!
Seria 23: https://questionpro.com/t/AT4qgZvALu
17
Foldr s, i Foldl
foldr s, i foldl
18
foldr s, i foldl
Funct, ia foldr
f o l d r : : ( a −> b −> b ) −> b −> [ a ] −> b
foldr f i [ ] = i
f o l d r f i ( x : xs ) = f x ( f o l d r f i xs )
Funct, ia foldl
f o l d l : : ( b −> a −> b ) −> b −> [ a ] −> b
foldl h i [ ] = i
f o l d l h i ( x : xs ) = f o l d l h ( h i x ) xs
19
Suma elementelor dintr-o listă
Solut, ie cu foldr
sum = f o l d r ( + ) 0
20
Suma elementelor dintr-o listă
sum : : [ I n t ] −> I n t
sum xs = suml xs 0
where
suml [ ] n = n
suml ( x : xs ) n = suml xs ( n+x )
suml [x1 , . . . , xn ] 0 = (. . . (0 + x1 ) + x2 ) + . . . xn )
Solut, ie cu foldl
sum : : [ I n t ] −> I n t
sum xs = f o l d l ( + ) 0 xs
21
Inversarea elementelor unei liste
Solut, ie cu foldl
−− f l i p : : ( a −> b −> c ) −> ( b −> a −> c )
−− ( : ) : : a −> [ a ] −> [ a ]
−− f l i p ( : ) : : [ a ] −> a −> [ a ]
22
Evaluare lenes, ă. Liste infinite
s i e v e [ 2 . . ] −−>
2 : 3 : s i e v e ( [ y | y <−
[ x | x <− [ 4 . . ] , mod x 2 / = 0 ] ,
mod y 3 / = 0 ] )
−−> . . . 24
foldr s, i foldl
https://en.wikipedia.org/wiki/Fold_(higher-order_function)
25
foldr s, i foldl
Prelude > f o l d r ( * ) 0 [ 1 . . ]
*** Exception: stack overflow
26
Quiz time!
Seria 23: https://questionpro.com/t/AT4qgZvHue
27
Pe săptămâna viitoare!
28