Created
August 7, 2020 20:37
-
-
Save herrhotzenplotz/1a5c900b7ceba78fc68e4fc6f9a943ff to your computer and use it in GitHub Desktop.
Word jumbler
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
module Main where | |
import System.Random | |
import System.Environment | |
import Control.Monad | |
import Data.Array.IO | |
main :: IO () | |
main = do | |
[name, num] <- getArgs | |
mapM shuffle (replicate (read num) name) >>= putStrLn . unlines | |
where | |
-- stolen from: https://wiki.haskell.org/Random_shuffle | |
shuffle :: [a] -> IO [a] | |
shuffle xs = do | |
ar <- newArray n xs | |
forM [1..n] $ \i -> do | |
j <- randomRIO (i,n) | |
vi <- readArray ar i | |
vj <- readArray ar j | |
writeArray ar j vi | |
return vj | |
where | |
n = length xs | |
newArray :: Int -> [a] -> IO (IOArray Int a) | |
newArray n xs = newListArray (1,n) xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment