Skip to content

Instantly share code, notes, and snippets.

@bavardage
Created December 30, 2009 15:03
Show Gist options
  • Select an option

  • Save bavardage/266108 to your computer and use it in GitHub Desktop.

Select an option

Save bavardage/266108 to your computer and use it in GitHub Desktop.
type Perm = [Int]
data Parity = Odd | Even deriving Show
oddtest = [1,3,2,4,5] :: Perm
eventest = [1,4,2,3,5] :: Perm
genPairs :: Int -> [(Int, Int)]
genPairs l = [(a,b) | a <- [1..l], b <- [1..l], a < b]
inversion :: Perm -> (Int,Int) -> Bool
inversion p (x,y) = o x > o y
where
o a = p !! (a-1)
countInversions p = length $ filter id $ map (inversion p) $ genPairs $ length p
parity :: Perm -> Parity
parity p | even $ countInversions p = Even
| otherwise = Odd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment