Skip to content

Instantly share code, notes, and snippets.

@profil
Last active August 29, 2015 14:05
Show Gist options
  • Save profil/b66f4a727d1a171fc788 to your computer and use it in GitHub Desktop.
Save profil/b66f4a727d1a171fc788 to your computer and use it in GitHub Desktop.
r/dailyprogrammer #175
import System.Environment (getArgs)
import System.Random
import Data.List (permutations)
shuffle :: String -> IO String
shuffle xs = do
gen <- newStdGen
let (randIndex, _) = randomR (0, length ys - 1) gen
return $ ys !! randIndex
where
ys = permutations xs
bogo :: String -> String -> Int -> IO String
bogo str sorted acc = case str == sorted of
True -> return $ show acc ++ " iterations"
False -> do
newStr <- shuffle str
bogo newStr sorted (acc+1)
main = do
args <- getArgs
case args of
[str, sorted] -> do
answer <- bogo str sorted 0
putStrLn answer
_ -> do
putStrLn "need two arguments"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment