programación funcional en haskell
Post on 27-Jun-2015
1.260 Views
Preview:
TRANSCRIPT
Programacion funcional en Haskell
Roberto Bonvallet
Departamento de InformaticaUniversidad Tecnica Federico Santa Marıa
Mayo de 2009
Programas y diapositivas
git clone git://github.com/rbonvall/charla-haskell.git
¿Que hace esto?
f l o a t z = 0 . 0 ;for ( i = 0 ; i < n ; ++ i )
z = z + a [ i ] ;
¿Y esto?
unsigned i n t z = 0 ;for ( i = 0 ; i < n ; ++ i )
z = MAX( z , a [ i ] ) ;
¿Que hace esto?
f l o a t z = 0 . 0 ;for ( i = 0 ; i < n ; ++ i )
z = z + a [ i ] ;
¿Y esto?
unsigned i n t z = 0 ;for ( i = 0 ; i < n ; ++ i )
z = MAX( z , a [ i ] ) ;
¿Y esto otro?
bool z = f a l s e ;for ( i = 0 ; i < n ; ++ i )
z = z | | ( a [ i ] % 2 == 0 ) ;
¿Y esto de aca?
s t r i n g z ( ”” ) ;for ( i = 0 ; i < n ; ++ i )
z . append ( a [ i ] ) ;
¿Y esto otro?
bool z = f a l s e ;for ( i = 0 ; i < n ; ++ i )
z = z | | ( a [ i ] % 2 == 0 ) ;
¿Y esto de aca?
s t r i n g z ( ”” ) ;for ( i = 0 ; i < n ; ++ i )
z . append ( a [ i ] ) ;
El patron comun
z = z0 ;for ( i = 0 ; i < n ; ++ i )
z = f(z , a [ i ] ) ;
En “funcional”
sum ’ [ ] = 0 . 0sum ’ ( x : xs ) = x + sum ’ xs
maximum’ [ ] = 0maximum’ ( x : xs ) = max x (maximum’ xs )
any even ’ [ ] = Falseany even ’ ( x : xs ) = ( x ‘mod‘ 2 == 0) | |
( any even ’ xs )
concat ’ [ ] = ””concat ’ ( x : xs ) = x ++ ( concat ’ xs )
Funcion foldl
sum ’ ’ = f o l d l ( + ) 0 . 0maximum’ ’ = f o l d l max 0concat ’ ’ = f o l d l (++) ””any even ’ ’ = f o l d l (λ x y →
x | | even y ) False
Definicion de foldl
f o l d l : : ( a → b → a ) → a → [ b ] → af o l d l f z0 [ ] = z0f o l d l f z0 ( x : xs ) = f o l d l f ( f z0 x ) xs
Definicion de funciones
f a c t 0 = 1f a c t x = x ∗ f a c t ( x − 1)
Operadores son funciones
41 ∗ 200( ∗ ) 41 200
Funciones son operadores
elem 2 [ 1 . . 1 0 ]2 ‘ elem ‘ [ 1 . . 1 0 ]
Definicion de funciones
f a c t 0 = 1f a c t x = x ∗ f a c t ( x − 1)
Operadores son funciones
41 ∗ 200( ∗ ) 41 200
Funciones son operadores
elem 2 [ 1 . . 1 0 ]2 ‘ elem ‘ [ 1 . . 1 0 ]
Definicion de funciones
f a c t 0 = 1f a c t x = x ∗ f a c t ( x − 1)
Operadores son funciones
41 ∗ 200( ∗ ) 41 200
Funciones son operadores
elem 2 [ 1 . . 1 0 ]2 ‘ elem ‘ [ 1 . . 1 0 ]
Quicksort
p
< p p ≥ p
En Haskell
qs [ ] = [ ]qs ( x : xs ) = qs ( f i l t e r (<x ) xs ) ++
[ x ] ++qs ( f i l t e r (≥x ) xs )
Quicksort
p
< p p ≥ p
En Haskell
qs [ ] = [ ]qs ( x : xs ) = qs ( f i l t e r (<x ) xs ) ++
[ x ] ++qs ( f i l t e r (≥x ) xs )
Dıgito verificador como si estuviera en primero
1 4 2 3 0 1 2 4
Dıgito verificador como si estuviera en primero
1 4 2 3 0 1 2 43 2 7 6 5 4 3 2
Dıgito verificador como si estuviera en primero
1 4 2 3 0 1 2 4× 3 2 7 6 5 4 3 2
3 8 14 18 0 4 6 8
Dıgito verificador como si estuviera en primero
1 4 2 3 0 1 2 4× 3 2 7 6 5 4 3 2
3 8 14 18 0 4 6 8+−−−−→ 61
Dıgito verificador como si estuviera en primero
1 4 2 3 0 1 2 4× 3 2 7 6 5 4 3 2
3 8 14 18 0 4 6 8+−−−−→ 61 11−x−−−→ −50
Dıgito verificador como si estuviera en primero
1 4 2 3 0 1 2 4× 3 2 7 6 5 4 3 2
3 8 14 18 0 4 6 8+−−−−→ 61 11−x−−−→ −50 mod 11−−−−−→ 5
Fibonacci vieja escuela
f i b 0 = 1f i b 1 = 1f i b x = f i b ( x − 1) + f i b ( x − 2)
Fibonacci perezoso
f i b = 1 : 1 : ( zipWith ( + ) f i b $ t a i l f i b )
Fibonacci vieja escuela
f i b 0 = 1f i b 1 = 1f i b x = f i b ( x − 1) + f i b ( x − 2)
Fibonacci perezoso
f i b = 1 : 1 : ( zipWith ( + ) f i b $ t a i l f i b )
Dıgito verificador, version 1
dv1 : : String → Chardv1 rut =
l e t revRut = reverse rutr e v D i g i t s = map digi tToInt revRutf a c t o r s = cyc le [ 2 . . 7 ]products =
zipWith ( ∗ ) f a c t o r s r e v D i g i t ssumOfProducts = sum productsd i g i t = (11 − sumOfProducts ) ‘mod‘ 11
in digitToChar d i g i twhere digitToChar 10 = ’k ’
digitToChar x = chr $ x + ord ’ 0 ’
Dıgito verificador, algunas simplificaciones
dv2 : : String → Chardv2 rut =
l e t r e v D i g i t s =map digi tToInt $ reverse rut
products =zipWith ( ∗ ) ( cycle [ 2 . . 7 ] ) r e v D i g i t s
d i g i t = (11 − sum products ) ‘mod‘ 11in ” 0123456789k” ! ! d i g i t
Dıgito verificador, mas astuto de la cuenta
dv3 : : String → Chardv3 rut = ”0 k987654321 ” ! ! ( ( sum $
zipWith ( ∗ ) ( cycle [ 2 . . 7 ] ) $map digi tToInt $reverse rut ) ‘mod‘ 11 )
Composicion
f : : a → bg : : b → c( f ◦ g ) : : a → c
Currying
zipWith : : ( a → b → c ) → [ a ] → [ b ] → [ c ]zipWith ( + ) : : (Num a ) ⇒
[ a ] → [ a ] → [ a ]zipWith ( + ) [ 1 . . 1 0 ] : : (Enum a , Num a ) ⇒
[ a ] → [ a ]
Composicion
f : : a → bg : : b → c( f ◦ g ) : : a → c
Currying
zipWith : : ( a → b → c ) → [ a ] → [ b ] → [ c ]zipWith ( + ) : : (Num a ) ⇒
[ a ] → [ a ] → [ a ]zipWith ( + ) [ 1 . . 1 0 ] : : (Enum a , Num a ) ⇒
[ a ] → [ a ]
Dıgito verificador, composicion + currying
dv4 : : String → Chardv4 rut = ”0 k987654321 ” ! ! ( (
(sum ◦ zipWith ( ∗ ) ( cyc le [ 2 . . 7 ] )◦ map digi tToInt ◦ reverse ) rut )
‘mod‘ 11 )
Dıgito verificador, propiedades de mod 11
dv5 : : String → Chardv5 rut = ( cyc le ”0 k987654321 ” ) ! ! (
(sum ◦ zipWith ( ∗ ) ( cyc le [ 2 . . 7 ] )◦ map digi tToInt ◦ reverse ) rut )
Dıgito verificador point-free
dv6 : : String → Chardv6 = ( ! ! ) ( cyc le ”0 k987654321 ” )
◦ sum ◦ zipWith ( ∗ ) ( cycle [ 2 . . 7 ] )◦ map digi tToInt ◦ reverse
Tipos de datos algebraicos
data Point = Point Float Floatdata Shape = C i r c l e Point Float
| Rectangle Point Point
s u r f a c e : : Shape → Floats u r f a c e ( C i r c l e r ) = pi ∗ r ˆ 2s u r f a c e ( Rectangle ( Point x1 y1 )
( Point x2 y2 ) ) =( abs $ x2 − x1 ) ∗ ( abs $ y2 − y1 )
Tipos parametrizados
data Tree a = EmptyTree| Node a ( Tree a ) ( Tree a )deriving (Show , Read , Eq )
s i n g l e t o n : : a → Tree as i n g l e t o n x = Node x EmptyTree EmptyTree
t r e e I n s e r t : : ( Ord a ) ⇒ a → Tree a → Tree at r e e I n s e r t x EmptyTree = s i n g l e t o n xt r e e I n s e r t x (Node a l e f t r i g h t )| x == a = Node x l e f t r i g h t| x < a = Node a ( t r e e I n s e r t x l e f t ) r i g h t| x > a = Node a l e f t ( t r e e I n s e r t x r i g h t )
Recursos
Haskell Wikihttp://www.haskell.org/haskellwiki/
Learn You a Haskell For Great Good!http://learnyouahaskell.com/
Real World Haskellhttp://book.realworldhaskell.org/read/
Haskell Cafehttp://news.gmane.org/gmane.comp.lang.haskell.cafe
top related