キヨシ
界隈の人たちはみんなポエムを書くのが好きなので,散々既出だと思うけど,先週京都にいるときに少し話が出て,自分だったらこう書くかなぁと,フワッと思ったものを書き下してみた.
import Data.Bool (bool) import Data.List (isPrefixOf) import System.Random (randomIO) data Kiyoshi = Z | D | K deriving Eq instance Show Kiyoshi where show Z = "ズン" show D = "ドコ" show K = "キ・ヨ・シ!" gen :: IO Kiyoshi gen = bool Z D <$> randomIO test :: [Kiyoshi] -> Bool test = isPrefixOf [D, Z, Z, Z, Z] main :: IO () main = let go xs = do x <- gen print x let xxs = x:xs if test xxs then print K else go xxs in go []
2016年5月23日追記
fix
を使った無名再帰で main
を書き直してみた.
あと,せっかく bool
を import しているので,最後で if を使うのをやめた.
import Control.Monad.Fix (fix) import Data.Bool (bool) import Data.List (isPrefixOf) import System.Random (randomIO) data Kiyoshi = Z | D | K deriving Eq instance Show Kiyoshi where show Z = "ズン" show D = "ドコ" show K = "キ・ヨ・シ!" gen :: IO Kiyoshi gen = bool Z D <$> randomIO test :: [Kiyoshi] -> Bool test = isPrefixOf [D, Z, Z, Z, Z] main :: IO () main = flip fix [] $ \loop xs -> do x <- gen print x let xxs = x:xs bool (loop xxs) (print K) (test xxs)