Created
June 30, 2013 19:30
-
-
Save Beyamor/5896529 to your computer and use it in GitHub Desktop.
Verb the Noun generator in Haskell
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
import Data.Char (toUpper) | |
import Control.Monad (liftM, mapM_) | |
import System.Random | |
readLines :: String -> IO [String] | |
readLines = liftM lines . readFile | |
boundaries :: [a] -> (Int, Int) | |
boundaries xs = (0, length xs - 1) | |
randomElement :: StdGen -> [a] -> (StdGen, a) | |
randomElement generator xs = let (index, generator') = randomR (boundaries xs) generator | |
x = xs !! index | |
in (generator', x) | |
randomPair :: StdGen -> [a] -> [b] -> (StdGen, (a, b)) | |
randomPair generator xs ys = let (generator', x) = randomElement generator xs | |
(generator'', y) = randomElement generator' ys | |
in (generator'', (x, y)) | |
randomPairs :: StdGen -> [a] -> [b] -> [(a, b)] | |
randomPairs generator xs ys = (x, y) : morePairs | |
where (generator', (x, y)) = randomPair generator xs ys | |
morePairs = randomPairs generator' xs ys | |
title :: String -> String | |
title (x:xs) = (toUpper x):xs | |
main = do | |
verbs <- readLines "verbs.txt" | |
nouns <- readLines "nouns.txt" | |
generator <- newStdGen | |
let pairs = take 100 $ randomPairs generator verbs nouns | |
mapM_ (\(verb, noun) -> putStrLn $ (title verb) ++ " the " ++ (title noun)) pairs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment