Programación
Funcional en Haskell
2a parte
Agustín Ramos Fonseca
Un lenguaje de programación...


• Funcional.

Las funciones son construcciones de primera clase.


• Puro.

Las funciones...
Características
•
•
•
•
•
•
•
•
•

List comprehensions.

Pattern matching.

Currying

Function Composition

Infinite data s...
Ejemplos
¿Haskell Sweet Spot?
Algoritmos
Eight Queens
Eight Queens
!
isValidSolution sol

= all isValid fullPositions


where fullPositions = zip sol [1..8]

isValid pos

= nul...
Knights Tour
Knights Tour
type Position = (Int,Int)

type Solution = [Position]

!

possibleMovs :: Int -> Position -> [Position]

poss...
Knights Tour
tours :: Int -> Position -> [Solution] 

tours n from

= tours' n from [[from]]


where tours' n from sols

=...
Lattice Paths

http://projecteuler.net/problem=15
Lattice Paths
import Control.Monad.Memo


!
pathsm 0 0 = return 0

pathsm 0 _ = return 1

pathsm _ 0 = return 1

pathsm r ...
Proyectos Interesantes
HAXL

http:/
/j.mp/1bWl9Oz
Elm

http:/
/elm-lang.org/
Porque...

#SoyPunk
¿Preguntas?
¡Gracias!

Agustín Ramos Fonseca

@MachinesAreUs
Upcoming SlideShare
Loading in...5
×

Programación funcional con Haskell

1,169

Published on

Haskell es un lenguaje funcional puro que ha encontrado una pequeña pero creciente y leal cantidad de usuarios en la industria y academia. Calificándolo como "El más avanzado de los lenguajes obsoletos", Gerlad J. Sussman (creador de Scheme) señala tanto al poder de Haskell como a las limitaciones de todos los lenguajes de programación existentes. ¿Qué lo hace especial o atractivo? ¿Vale la pena invertir nuestro tiempo en aprenderlo?

Este webinar es la continuación de la conferencia que se dio durante el pasado SGVirtual.

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,169
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Programación funcional con Haskell

  1. 1. Programación Funcional en Haskell 2a parte Agustín Ramos Fonseca
  2. 2. Un lenguaje de programación... • Funcional. Las funciones son construcciones de primera clase. • Puro. Las funciones no tienen efectos colaterales. • De evaluación perezosa. Las expresiones se evalúan hasta que es requerido. • De tipado estático. Con inferencia de tipos.
  3. 3. Características • • • • • • • • • List comprehensions. Pattern matching. Currying Function Composition Infinite data structures. Agebraic Data Types Type classes. Applicative Functors Monads
  4. 4. Ejemplos
  5. 5. ¿Haskell Sweet Spot?
  6. 6. Algoritmos
  7. 7. Eight Queens
  8. 8. Eight Queens ! isValidSolution sol = all isValid fullPositions where fullPositions = zip sol [1..8] isValid pos = null $ intersect fullPositions $ diags pos diags pos = diag1 pos ++ diag2 pos ++ diag3 pos ++ diag4 pos diag1 (r,c) = [(r',c') | r' <- [r-1,r-2..1], r' >= 1, c' <- [c-1,c-2..1], c' >= 1, r'-c' == r-c ] diag2 (r,c) = [(r',c') | r' <- [r+1..8], r' <= 8, c' <- [c+1..8], diag3 (r,c) = [(r',c') | r' <- [r+1..8], r' <= 8, c' <- [c-1,c-2..1], c' >= 1, r'+c' == r+c ] diag4 (r,c) = [(r',c') | r' <- [r-1,r-2..1], r' >= 1, c' <- [c+1..8], ! eightQueens = filter isValidSolution $ permutations [1..8] c' <= 8, r'-c' == r-c ] c' <= 8, r'+c' == r+c ]
  9. 9. Knights Tour
  10. 10. Knights Tour type Position = (Int,Int) type Solution = [Position] ! possibleMovs :: Int -> Position -> [Position] possibleMovs n (r,c) = [(r',c') | r' <- [r-2..r+2], c' <- [c-2..c+2], isLMovement (r’,c'), isWithinBoard (r',c')] where isLMovement (r',c') = abs(r-r') + abs(c-c') == 3 isWithinBoard (r',c') = r' >= 1 && r' <= n && c' >= 1 && c' <= n
  11. 11. Knights Tour tours :: Int -> Position -> [Solution] tours n from = tours' n from [[from]] where tours' n from sols = filter fullTour $ concat $ solutions n from sols solutions n from sols = parMap rpar (completeSols sols) $ possibleMovs n from completeSols sols m = if isNewPosition m sols then tours' n m $ appendToSolutions sols m else sols fullTour s = length s == n*n isNewPosition p appendToSolutions [] = all (isNothing . elemIndex p) mov = [[mov]] appendToSolutions sols mov = map (sol -> sol ++ [mov] ) sols ! knightsTour n = concat $ parMap rpar (tours n) allPositions where allPositions = [(r,c) | r <- [1..n], c <- [1..n]]
  12. 12. Lattice Paths http://projecteuler.net/problem=15
  13. 13. Lattice Paths import Control.Monad.Memo ! pathsm 0 0 = return 0 pathsm 0 _ = return 1 pathsm _ 0 = return 1 pathsm r d = do r1 <- for2 memo pathsm (r - 1) d d1 <- for2 memo pathsm r (d -1) return (r1 + d1) ! evalPathsm :: (Num n, Ord n) => n -> n -> n evalPathsm r d = startEvalMemo $ pathsm r d ! runPathsm n = evalPathsm n n
  14. 14. Proyectos Interesantes
  15. 15. HAXL http:/ /j.mp/1bWl9Oz
  16. 16. Elm http:/ /elm-lang.org/
  17. 17. Porque... #SoyPunk
  18. 18. ¿Preguntas?
  19. 19. ¡Gracias! Agustín Ramos Fonseca @MachinesAreUs
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×