Skip to content

Instantly share code, notes, and snippets.

@khibino
Last active December 14, 2015 21:18
Show Gist options
  • Save khibino/5149966 to your computer and use it in GitHub Desktop.
Save khibino/5149966 to your computer and use it in GitHub Desktop.
import Data.List (isPrefixOf)
import Data.Set (Set)
import qualified Data.Set as Set
data K' = K0 | K1
deriving (Show, Eq, Ord)
data K = Unit K'
| Str !String
deriving Show
instance Eq K where
Str _ == Str _ = True
Str _ == Unit _ = False
Unit _ == Str _ = False
Unit a == Unit b = a == b
-- instance Ord K where
-- Str _ < Str _ = False
-- Str _ < Unit _ = False
-- Unit _ < Str _ = True
-- Unit a < Unit b = a < b
instance Ord K where
Str _ `compare` Str _ = EQ
Str _ `compare` Unit _ = LT
Unit _ `compare` Str _ = GT
Unit a `compare` Unit b = a `compare` b
kOfString :: String -> K
kOfString = d where
d str
| "0" `isPrefixOf` str = Unit K0
| "1" `isPrefixOf` str = Unit K1
| otherwise = Str str
type Key = K
datas :: [Key]
datas = cycle $ map (kOfString . show) ([0,0,1,0,1,1,0,0,0,1] :: [Int])
ngroup :: Int -> [a] -> [[a]]
ngroup n = rec where
rec xs = hd : rec xs' where
(hd, xs') = splitAt n xs
chunks :: [[[Key]]]
chunks = ngroup 5 . ngroup 3 $ datas
maps :: [Set Key]
maps = map (Set.unions . map Set.fromList) chunks
run :: [Set Key]
run = take 10 maps
main :: IO ()
main = mapM_ print run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment