Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Programación funcional en Haskell

Programación funcional en Haskell

Roberto Bonvallet

May 07, 2009
Tweet

More Decks by Roberto Bonvallet

Other Decks in Programming

Transcript

  1. Programaci´ on funcional en Haskell Roberto Bonvallet Departamento de Inform´

    atica Universidad T´ ecnica Federico Santa Mar´ ıa Mayo de 2009
  2. ¿Qu´ e hace esto? float z = 0 . 0

    ; for ( i = 0 ; i < n ; ++ i ) z = z + a [ i ] ;
  3. ¿Qu´ e hace esto? float z = 0 . 0

    ; for ( i = 0 ; i < n ; ++ i ) z = z + a [ i ] ; ¿Y esto? unsigned int z = 0 ; for ( i = 0 ; i < n ; ++ i ) z = MAX( z , a [ i ] ) ;
  4. ¿Y esto otro? bool z = false ; for (

    i = 0 ; i < n ; ++ i ) z = z | | ( a [ i ] % 2 == 0 ) ;
  5. ¿Y esto otro? bool z = false ; for (

    i = 0 ; i < n ; ++ i ) z = z | | ( a [ i ] % 2 == 0 ) ; ¿Y esto de ac´ a? string z ( ”” ) ; for ( i = 0 ; i < n ; ++ i ) z . append ( a [ i ] ) ;
  6. El patr´ on com´ un z = z0 ; for

    ( i = 0 ; i < n ; ++ i ) z = f(z , a [ i ]) ;
  7. En “funcional” sum’ [ ] = 0.0 sum’ ( x

    : xs ) = x + sum’ xs maximum’ [ ] = 0 maximum’ ( x : xs ) = max x (maximum’ xs ) any even ’ [ ] = False any even ’ ( x : xs ) = ( x ‘mod‘ 2 == 0) | | ( any even ’ xs ) concat ’ [ ] = ”” concat ’ ( x : xs ) = x ++ ( concat ’ xs )
  8. Funci´ on foldl sum’ ’ = foldl (+) 0.0 maximum’

    ’ = foldl max 0 concat ’ ’ = foldl (++) ”” any even ’ ’ = foldl (λ x y → x | | even y ) False
  9. Definici´ on de foldl foldl : : ( a →

    b → a ) → a → [ b ] → a foldl f z0 [ ] = z0 foldl f z0 ( x : xs ) = foldl f ( f z0 x ) xs
  10. Definici´ on de funciones f a c t 0 =

    1 f a c t x = x ∗ f a c t ( x − 1)
  11. Definici´ on de funciones f a c t 0 =

    1 f a c t x = x ∗ f a c t ( x − 1) Operadores son funciones 41 ∗ 200 ( ∗ ) 41 200
  12. Definici´ on de funciones f a c t 0 =

    1 f 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 ]
  13. 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 )
  14. 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
  15. 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
  16. 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
  17. 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 m´ od 11 − − − − − → 5
  18. Fibonacci vieja escuela f i b 0 = 1 f

    i b 1 = 1 f i b x = f i b ( x − 1) + f i b ( x − 2)
  19. Fibonacci vieja escuela f i b 0 = 1 f

    i b 1 = 1 f 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 )
  20. D´ ıgito verificador, versi´ on 1 dv1 : : String

    → Char dv1 rut = l e t revRut = reverse rut revDigits = map digitToInt revRut f a c t o r s = cycle [ 2 . . 7 ] products = zipWith ( ∗ ) f a c t o r s revDigits sumOfProducts = sum products d i g i t = (11 − sumOfProducts ) ‘mod‘ 11 in digitToChar d i g i t where digitToChar 10 = ’k ’ digitToChar x = chr $ x + ord ’0 ’
  21. D´ ıgito verificador, algunas simplificaciones dv2 : : String →

    Char dv2 rut = l e t revDigits = map digitToInt $ reverse rut products = zipWith ( ∗ ) ( cycle [ 2 . . 7 ] ) revDigits d i g i t = (11 − sum products ) ‘mod‘ 11 in ”0123456789k” ! ! d i g i t
  22. D´ ıgito verificador, m´ as astuto de la cuenta dv3

    : : String → Char dv3 rut = ”0k987654321” ! ! ( (sum $ zipWith ( ∗ ) ( cycle [ 2 . . 7 ] ) $ map digitToInt $ reverse rut ) ‘mod‘ 11)
  23. Composici´ on f : : a → b g :

    : b → c ( f ◦ g ) : : a → c
  24. Composici´ on f : : a → b g :

    : 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 ]
  25. D´ ıgito verificador, composici´ on + currying dv4 : :

    String → Char dv4 rut = ”0k987654321” ! ! ( ( (sum ◦ zipWith ( ∗ ) ( cycle [ 2 . . 7 ] ) ◦ map digitToInt ◦ reverse ) rut ) ‘mod‘ 11)
  26. D´ ıgito verificador, propiedades de mod 11 dv5 : :

    String → Char dv5 rut = ( cycle ”0k987654321” ) ! ! ( (sum ◦ zipWith ( ∗ ) ( cycle [ 2 . . 7 ] ) ◦ map digitToInt ◦ reverse ) rut )
  27. D´ ıgito verificador point-free dv6 : : String → Char

    dv6 = ( ! ! ) ( cycle ”0k987654321” ) ◦ sum ◦ zipWith ( ∗ ) ( cycle [ 2 . . 7 ] ) ◦ map digitToInt ◦ reverse
  28. Tipos de datos algebraicos data Point = Point Float Float

    data Shape = Circle Point Float | Rectangle Point Point surface : : Shape → Float surface ( Circle r ) = pi ∗ r ˆ 2 surface ( Rectangle ( Point x1 y1 ) ( Point x2 y2 ) ) = ( abs $ x2 − x1 ) ∗ ( abs $ y2 − y1 )
  29. Tipos parametrizados data Tree a = EmptyTree | Node a

    ( Tree a ) ( Tree a ) deriving (Show, Read , Eq) singleton : : a → Tree a singleton x = Node x EmptyTree EmptyTree t r e e I n s e r t : : (Ord a ) ⇒ a → Tree a → Tree a t r e e I n s e r t x EmptyTree = singleton x t r e e I n s e r t x (Node a l e f t right ) | x == a = Node x l e f t right | x < a = Node a ( t r e e I n s e r t x l e f t ) right | x > a = Node a l e f t ( t r e e I n s e r t x right )
  30. Recursos Haskell Wiki http://www.haskell.org/haskellwiki/ Learn You a Haskell For Great

    Good! http://learnyouahaskell.com/ Real World Haskell http://book.realworldhaskell.org/read/ Haskell Cafe http://news.gmane.org/gmane.comp.lang. haskell.cafe