programación funcional en haskell

34
Programaci ´ on funcional en Haskell Roberto Bonvallet Departamento de Inform ´ atica Universidad T´ ecnica Federico Santa Mar´ ıa Mayo de 2009

Upload: roberto-bonvallet

Post on 27-Jun-2015

1.260 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Programación funcional en Haskell

Programacion funcional en Haskell

Roberto Bonvallet

Departamento de InformaticaUniversidad Tecnica Federico Santa Marıa

Mayo de 2009

Page 2: Programación funcional en Haskell

Programas y diapositivas

git clone git://github.com/rbonvall/charla-haskell.git

Page 3: Programación funcional en Haskell

¿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 ] ) ;

Page 4: Programación funcional en Haskell

¿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 ] ) ;

Page 5: Programación funcional en Haskell

¿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 ] ) ;

Page 6: Programación funcional en Haskell

¿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 ] ) ;

Page 7: Programación funcional en Haskell

El patron comun

z = z0 ;for ( i = 0 ; i < n ; ++ i )

z = f(z , a [ i ] ) ;

Page 8: Programación funcional en Haskell

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 )

Page 9: Programación funcional en Haskell

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

Page 10: Programación funcional en Haskell

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

Page 11: Programación funcional en Haskell

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 ]

Page 12: Programación funcional en Haskell

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 ]

Page 13: Programación funcional en Haskell

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 ]

Page 14: Programación funcional en Haskell

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 )

Page 15: Programación funcional en Haskell

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 )

Page 16: Programación funcional en Haskell

Dıgito verificador como si estuviera en primero

1 4 2 3 0 1 2 4

Page 17: Programación funcional en Haskell

Dıgito verificador como si estuviera en primero

1 4 2 3 0 1 2 43 2 7 6 5 4 3 2

Page 18: Programación funcional en Haskell

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

Page 19: Programación funcional en Haskell

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

Page 20: Programación funcional en Haskell

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

Page 21: Programación funcional en Haskell

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

Page 22: Programación funcional en Haskell

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 )

Page 23: Programación funcional en Haskell

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 )

Page 24: Programación funcional en Haskell

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 ’

Page 25: Programación funcional en Haskell

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

Page 26: Programación funcional en Haskell

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 )

Page 27: Programación funcional en Haskell

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 ]

Page 28: Programación funcional en Haskell

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 ]

Page 29: Programación funcional en Haskell

Dıgito verificador, composicion + currying

dv4 : : String → Chardv4 rut = ”0 k987654321 ” ! ! ( (

(sum ◦ zipWith ( ∗ ) ( cyc le [ 2 . . 7 ] )◦ map digi tToInt ◦ reverse ) rut )

‘mod‘ 11 )

Page 30: Programación funcional en Haskell

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 )

Page 31: Programación funcional en Haskell

Dıgito verificador point-free

dv6 : : String → Chardv6 = ( ! ! ) ( cyc le ”0 k987654321 ” )

◦ sum ◦ zipWith ( ∗ ) ( cycle [ 2 . . 7 ] )◦ map digi tToInt ◦ reverse

Page 32: Programación funcional en Haskell

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 )

Page 33: Programación funcional en Haskell

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 )

Page 34: Programación funcional en Haskell

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