100ãã¹è¨ç®ã®ã·ã¼ããçæãã
ããºã«æ室ã«ã¦å¨ã®æ°å¦çã»ã³ã¹ããã¿è¤ãããããã®ã®ãè¨ç®é度ãé
ãã®ã§100ãã¹è¨ç®ã家æã§ããããã«ã¨æ示ãåããã
æ©éãPython ã§100ãã¹è¨ç®ã·ã¼ãã HTML å½¢å¼ã§åºåããã³ãã³ããä½æããã®ã ãã身è¿ã« Haskeller ãå±
ãã®ã ãã Haskell ã§æ¸ãã¦æ·»åãã¦ããããã¨æãç«ã£ããã±ã¼ãä¸åãã§è«ãè² ã£ã¦ãããã ãããï¼w;
ã¨ãããããæ·»ååã®ã³ã¼ããæãã¦ãããæ·»åå¾ã«ã¯ããã£ã¨ç¾ããé«æ©è½(ä¾ãã° HTML ã§åºåããæ©è½ãå ãã£ããâ¦)ã«ãªãäºå®ã
import System.Random import System.IO import Data.List gen_cells :: Integer -> Integer -> IO [String] gen_cells min max = do gen <- newStdGen return $ take 10 $ map (\n -> show n) $ randomRs (min, max) gen i2path :: Integer -> String i2path i = "./" ++ show i ++ ".txt" print_table :: String -> String -> [String] -> [String] -> IO () print_table path method x_cells y_cells = do let head = concat $ intersperse " " x_cells let head_line = method ++ " " ++ head let lines = head_line : y_cells withFile path WriteMode $ \handle -> do sequence_ $ map (hPutStrLn handle) lines mk_html' :: Integer -> String -> (Integer, Integer) -> (Integer, Integer) -> IO () mk_html' n method (x_min, x_max) (y_min, y_max) = do x_cells <- gen_cells x_min x_max y_cells <- gen_cells y_min y_max print_table (i2path n) method x_cells y_cells mk_html :: Integer -> IO () mk_html n | n `rem` 4 == 1 = mk_html' n "ï¼" (1, 99) (1, 99) | n `rem` 4 == 2 = mk_html' n "â" (50, 99) (0, 49) | n `rem` 4 == 3 = mk_html' n "Ã" (1, 99) (0, 9) | otherwise = mk_html' n "÷" (1, 99) (1, 9) main = sequence_ $ map mk_html [1..20]
æ©éããªãã¡ã¯ã¿ãã¦é ããã®ã§ã³ã¼ããå ¬é
{-# LANGUAGE TemplateHaskell, QuasiQuotes #-} import System.Random import System.FilePath import System.IO import Data.List import Control.Applicative import Text.Hamlet import Text.Blaze.Html.Renderer.String import Text.Blaze data Method = Plus | Minus | Mult | Div type Range = (Integer, Integer) type Cells = [Integer] type Answers a = [[a]] get_calc_fun :: Method -> Integer -> Integer -> Integer get_calc_fun Plus = (+) get_calc_fun Minus = (-) get_calc_fun Mult = (*) get_calc_fun Div = div method2str :: Method -> String method2str Plus = "ï¼" method2str Minus = "â" method2str Mult = "Ã" method2str Div = "÷" gen_cells :: Range -> IO Cells gen_cells range = (take 10 . randomRs range) <$> newStdGen calc_answer :: Method -> Cells -> Cells -> Answers Integer calc_answer method x_cells y_cells = let f = get_calc_fun method in [[f x y | x <- x_cells] | y <- y_cells] null_answer :: Answers Integer -> Answers String null_answer answers = map (map (const "")) answers i2path :: String -> Integer -> FilePath i2path p i = "." </> (p ++ show i) <.> "html" -- Integer ã String 㯠ToMarkup ã®ã¤ã³ã¹ã¿ã³ã¹ print_table :: ToMarkup a => FilePath -> Method -> Cells -> Cells -> Answers a -> IO () print_table path method x_cells y_cells answers = do let rows = zip y_cells answers writeFile path $ renderHtml [shamlet| !!! <head> <title>100cells <body> <table> <tr> <th>#{method2str method} $forall x <- x_cells <th>#{x} $forall (y, zs) <- rows <tr> <th>#{y} $forall z <- zs <td>#{z} |] mk_html' :: Integer -> Method -> Range -> Range-> IO () mk_html' n method x_range y_range = do x_cells <- gen_cells x_range y_cells <- gen_cells y_range let answers = calc_answer method x_cells y_cells print_table (i2path "a" n) method x_cells y_cells answers print_table (i2path "p" n) method x_cells y_cells $ null_answer answers mk_html :: Integer -> IO () mk_html n | n `rem` 4 == 1 = mk_html' n Plus ( 1, 99) (1, 99) | n `rem` 4 == 2 = mk_html' n Minus (50, 99) (0, 49) | n `rem` 4 == 3 = mk_html' n Mult ( 1, 99) (0, 9) | otherwise = mk_html' n Div ( 1, 99) (1, 9) main = mapM_ mk_html [1..20]