Created
June 6, 2018 02:22
-
-
Save TakashiHarada/a87115a792276a490c21cbd9e41643bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import System.Random.Shuffle | |
import System.Random | |
import System.Environment | |
import Data.List | |
main :: IO () | |
main = | |
getArgs >>= | |
\args -> if 3 /= (length args) | |
then putStrLn "Usage: $ ./mkC1Pmatrix 100 32 randomC1Pmatrix" | |
else | |
mkC1PMatrix (read $ head args) (read $ head $ tail args) >>= | |
\m -> shuffleM (transpose m) >>= | |
\m' -> writeFile (last args) (unlines $ transpose m') | |
mkC1PMatrix :: Int -> Int -> IO [String] | |
mkC1PMatrix 0 _ = return [] | |
mkC1PMatrix m n = mkC1PRow n >>= \r -> mkC1PMatrix (m-1) n >>= \rs -> return (r : rs) | |
mkC1PRow :: Int -> IO String | |
mkC1PRow w = | |
(randomRIO (1, w) :: IO Int) >>= \l -> -- l is the length of run | |
(randomRIO (0, w-l) :: IO Int) >>= \s -> -- s is the starting point of run | |
getStdGen >>= \gen -> | |
let r = take l $ repeat '1' in | |
return $ (take s $ repeat '0') ++ r ++ (take (w-s-l) $ repeat '0') | |
mkC1PRuleList :: Int -> Int -> IO [String] | |
mkC1PRuleList 0 _ = return [] | |
mkC1PRuleList n w = mkC1PRule w >>= \r -> mkC1PRuleList (n-1) w >>= \rs -> return (r : rs) | |
mkC1PRule :: Int -> IO String | |
mkC1PRule w = | |
(randomRIO (1, w) :: IO Int) >>= \l -> -- l is the length of run | |
(randomRIO (0, w-l) :: IO Int) >>= \s -> -- s is the starting point of run | |
getStdGen >>= \gen -> | |
let r = take l $ (randomRs ('0','1') gen) :: [Char] in | |
return $ (take s $ repeat '*') ++ r ++ (take (w-s-l) $ repeat '*') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment