Skip to content

Instantly share code, notes, and snippets.

@herrhotzenplotz
Created August 7, 2020 20:37
Show Gist options
  • Save herrhotzenplotz/1a5c900b7ceba78fc68e4fc6f9a943ff to your computer and use it in GitHub Desktop.
Save herrhotzenplotz/1a5c900b7ceba78fc68e4fc6f9a943ff to your computer and use it in GitHub Desktop.
Word jumbler
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