3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Haskellでもがんばるぞい!

Last updated at Posted at 2014-09-23

giiko_さんの
今日も1日がんばるぞい!
が可愛くて面白かったのでHaskell修行のお題に使わせて頂きました。

zoi.hs
import System.Random
import Control.Monad
import Text.Printf

dic :: [[String]]
dic = [["今日","ぞい"],["も"],["1","ぞい"],["日","ぞい"],["がん","ぞい"],["ばる","ぞい"],["ぞい!"]]

targetMes :: String
targetMes = "今日も1日がんばるぞい!"

main :: IO()
main = do lst1 <- tryMes []
          putStrLn $ unlines lst1
          printf "☆がんばるまで %d zoiでした☆\n" (length lst1)

tryMes :: [String] -> IO [String]
tryMes l = do mes <- makeMes
              if mes == targetMes
                  then return (l ++ [targetMes])
                  else tryMes (l ++ [mes])

makeMes :: IO String
makeMes = do wordLst <- mapM choice dic
             return (foldr (++) "" wordLst)
             where
               choice :: [String] -> IO String
               choice cand = do
                 idx <- (getStdRandom $ randomR (0, (length cand) - 1) :: IO Int)
                 return (cand !! idx)

えー、まあ、浅学非才修行中の身なればコードがアレゲにもほどがあるのはご容赦を……orz
あと絵文字はどうやったものか分からなかったので「☆」にしております。

しょうがくせいなみのかんそう

乱数が絡むとすごく……ややこしいです……orz
モナドが理解できればややこしくなくなるんでしょーか……

3
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?