Skip to content

Instantly share code, notes, and snippets.

@zaltoprofen
Created June 20, 2015 23:23
Show Gist options
  • Save zaltoprofen/13092e0a26af3e480531 to your computer and use it in GitHub Desktop.
Save zaltoprofen/13092e0a26af3e480531 to your computer and use it in GitHub Desktop.
import System.Random
power :: [a] -> [[a]]
power xs = let
go [] ys = ys
go (x:xs) ys = go xs $ map (\a -> a ++ [x]) ys ++ ys
in
go xs [[]]
pickRand :: RandomGen r => r -> [a] -> [a]
pickRand gen [] = []
pickRand gen (x:xs) = let
r :: Double
(r, genN) = random gen
in
if r < 0.5
then pickRand genN xs
else x : (pickRand genN xs)
printStrs :: [[Char]] -> IO ()
printStrs xs = let
pr :: [[Char]] -> IO ()
pr [] = return ()
pr (x:[]) = putStr $ "\"" ++ x ++ "\""
pr (x:xs) = do
_ <- putStr $ "\"" ++ x ++ "\", "
pr xs
in do
_ <- putStr "["
_ <- pr xs
putStrLn "]"
main :: IO ()
main = do
gen <- newStdGen
let x = pickRand gen $ power "寝る" in do
_ <- printStrs x
putStrLn $ case (length x) of
4 -> "寝る冪"
_ -> "寝る冪でない"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment