This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 13th, 2012  |  syntax: None  |  size: 0.84 KB  |  views: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. type Player = Integer
  2. type Card = Integer
  3.  
  4. type Hand = [Card]
  5. type Deck = Hand
  6. type Hands = Player -> Hand
  7.  
  8. deal' :: Player -> (Player -> Player) -> Deck -> [Hands -> Hands]
  9. deal' player next [] = []
  10. deal' player next (card : cards) = giveTo player card : deal' (next player) next cards
  11.  
  12. giveTo player card hands = \player' ->
  13.  if player == player'
  14.     then card : hands player'
  15.     else        hands player'
  16.  
  17. deal'' :: [Hands -> Hands] -> Hands
  18. deal'' [] = const []
  19. deal'' (action : actions) = action (deal'' actions)
  20.  
  21. deal players = deal'' . deal' 0 (\player -> (player + 1) `mod` players)
  22.  
  23. -- *Main> deal 3 [5,6,8,9,7,5,4,2,3,1,5,6,7] 0
  24. -- [5,9,4,1,7]
  25. -- *Main> deal 3 [5,6,8,9,7,5,4,2,3,1,5,6,7] 1
  26. -- [6,7,2,5]
  27. -- *Main> deal 3 [5,6,8,9,7,5,4,2,3,1,5,6,7] 2
  28. -- [8,5,3,6]
  29. -- *Main> deal 3 [5,6,8,9,7,5,4,2,3,1,5,6,7] 3
  30. -- []
clone this paste RAW Paste Data