Created
August 2, 2014 23:35
-
-
Save erewok/cdd68522ce03eb78e942 to your computer and use it in GitHub Desktop.
Taco Shop Namer 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
-- run to find out your taco shop name | |
import System.Random | |
import Control.Monad.State.Lazy | |
all_let :: [Char] | |
all_let = ['a'..'z'] | |
vowels :: [Char] | |
vowels = "aeiouy" | |
consonants :: [Char] | |
consonants = [x | x <- all_let, not $ x `elem` vowels] | |
getRandChar' :: [Char] -> IO Char | |
getRandChar' xs = do | |
x <- getStdRandom $ randomR (0, (length xs - 1)) | |
return (xs !! x) | |
buildVals :: Int -> [Int] | |
buildVals 0 = 1 : buildVals 1 | |
buildVals 1 = 0 : buildVals 0 | |
makeRandWord :: [Int] -> IO [Char] | |
makeRandWord ns = sequence [if n == 0 then getRandChar' vowels else getRandChar' consonants | n <- ns] | |
main = do | |
putStrLn "enter total length of taco shop name" | |
total_length <- getLine | |
let total = (read total_length :: Int) - 6 | |
g <- getStdGen | |
let first = fst $ randomR (0, 1) g | |
let randomizer = take total $ buildVals first | |
let newWord = makeRandWord randomizer | |
newName <- liftM2 (++) newWord ((return "bertos") :: IO [Char]) | |
-- Alternate (simpler): | |
-- fmap (++"bertos") newName | |
putStrLn (show newName) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment