Scalaパズル

を素早くHaskellで解く。

via http://d.hatena.ne.jp/kmizushima/20080826/1219723302

take3 []         = ""
take3 (a:b:[]  ) = (td_show a) ++ (td_show b) ++ td_nbsp ++ "  </tr>\n"
take3 (a:[]    ) = "  <tr>\n" ++ (td_show a) ++ td_nbsp ++ td_nbsp ++ "  </tr>\n"
take3 (a:b:c:d ) = "  <tr>\n" ++ (td_show a) ++ (td_show b) ++ (td_show c) ++ "  </tr>\n" ++ take3 d 

td_show int = "    <td>"++(show int)++"</td>\n"

td_nbsp = "    <td>&nbsp;</td>\n"

main = putStr $ "<table>\n" ++ take3 [1,2,3,4,5,6,7] ++ "</table>\n"
takeN :: Int -> [Int] -> [[String]]
takeN n list = divideN (padding' (map show list))
    where
      padding' list' = 
          if (mod (length list') n) == 0
          then list'
          else padding' (list'++["&nbsp;"])

      divideN :: [String] -> [[String]]
      divideN []   = []
      divideN list = [(take n list)] ++ (divideN $ drop n list)

spaces :: Int -> String
spaces n = take n $ cycle " "

enclose_td :: [String] -> String
enclose_td list = concat (map (\x -> spaces 4++"<td>"++x++"</td>\n") list)

enclose_tr :: [[String]] -> String
enclose_tr list = concat (map (\x -> spaces 2++"<tr>\n"++enclose_td x++spaces 2++"</tr>\n") list)

enclose_table :: [[String]] -> String
enclose_table list = "<table>\n" ++ enclose_tr list ++ "</table>\n"

main = putStr $ enclose_table (takeN 3 [1..20])

いや別段載せる意味も無いですが。


関係無いですが、ニコニコに中々ロマサガ3TASの続きが上がらないのが気になる。