Skip to content

Instantly share code, notes, and snippets.

@tmiller
Last active August 29, 2015 14:03
Show Gist options
  • Save tmiller/5871d98225341c5dec59 to your computer and use it in GitHub Desktop.
Save tmiller/5871d98225341c5dec59 to your computer and use it in GitHub Desktop.
Round robin tournament algorthim for generating coding pairs in Haskell
module Main where
import Data.List
data Coder = Coder String
| Nobody
deriving (Show)
data Pairings = Pair Coder Coder
| Pairs [Pairings]
deriving (Show)
main = putStrLn . intercalate "\n" . map renderPairings . pair . coders $ names
names :: [String]
names = ["Andrew","Anthony","Ben","Matt", "Tom","Zac"]
coders :: [String] -> [Coder]
coders cs | even (length cs) = map Coder cs
| otherwise = Nobody : map Coder cs
split :: [a] -> ([a],[a])
split [] = ([],[])
split xs = splitAt midpoint xs
where midpoint = (length xs) `div` 2
reverseSnd :: ([a],[a]) -> ([a],[a])
reverseSnd (top,bot) = (top, reverse bot)
rotate :: [a] -> [a]
rotate xs = last xs : init xs
pair :: [Coder] -> [Pairings]
pair (piv:cs) = map buildPairsings orderdDevRotations
where buildPairsings = Pairs . (uncurry $ zipWith Pair) . reverseSnd . split
orderdDevRotations = take (length cs) rotations
rotations = map (piv:) (iterate rotate cs)
renderCoder :: Coder -> String
renderCoder (Coder c) = filter (/= '\"') $ show c
renderCoder (Nobody) = "Nobody"
renderPairings :: Pairings -> String
renderPairings (Pair a b) = (renderCoder a) ++ " & " ++ (renderCoder b)
renderPairings (Pairs ps) = (show $ map renderPairings ps)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment