ピラミッド

ひきつづき HUnit の練習。TDD、TDD。

import Test.HUnit

nrepeat :: Int -> b -> [b]
nrepeat n c = take n (repeat c)

pyramid :: Int -> String
pyramid n = pyramid' 1 (n - 1)
  where
    pyramid' _ s | s < 0 = []
    pyramid' i s = nrepeat s ' ' ++ nrepeat i '*' ++ "\n" ++ pyramid' (i + 2) (s - 1)

testData :: [Test]
testData = map (\(input, expected) -> expected ~=? pyramid input) [
             (4, "   *\n  ***\n *****\n*******\n"),
             (0, ""),
             (1, "*\n"),
             (2, " *\n***\n")
           ]

main :: IO Counts
main = runTestTT (test testData)