In today’s Programming Praxis exercise, our goal is to generate xkcd-style passphrases consisting of four words. Let’s get started, shall we?
import Data.Char import System.Random.Shuffle
First, we define our criteria for acceptable words, in this case lowercase words between 5 and 9 letters.
valid :: String -> Bool valid s = length s > 4 && length s < 10 && all isLower s
Generating a passphrase is then a simple matter of loading a dictionary, filtering the valid words, shuffling them and printing the first four chosen words.
main :: IO () main = putStrLn . unwords . take 4 =<< shuffleM . filter valid . lines =<< readFile "en_US.dic"