Skip to content

Instantly share code, notes, and snippets.

@pogin503
Created April 25, 2012 14:57
Show Gist options
  • Select an option

  • Save pogin503/2490357 to your computer and use it in GitHub Desktop.

Select an option

Save pogin503/2490357 to your computer and use it in GitHub Desktop.
九九のプログラム
-- @see http://d.hatena.ne.jp/wvogel00/20110528/1306599983
-- main = putStr $ unlines $ map show $ map (kuku 1 []) [1..9]
kuku :: Int -> [Int] -> Int -> [Int]
kuku 9 xs n = xs ++ [9*n]
kuku x xs n = kuku (x + 1) (xs ++ [n*x]) n
putKuku = putStr $ unlines $ map show $ map (kuku 1 []) [1..9]
-- @see http://d.hatena.ne.jp/kazu-yamamoto/20080215/1203060588
row = 9
col = 9
kuku1 [] lst = []
kuku1 (n:ns) lst = map (\x -> n * x) lst : kuku1 ns lst
-- [1..3] [1..3] -> [[1,2,3],[2,4,6],[3,6,9]]
table = kuku1 [1..9] [1..9]
rowToLine = concatMap (\n -> (if n < 10 then " " else "") ++ (show n) ++ " ")
-- rowToLine [1,2,3] -> "1 2 3"
display lst = putStr $ unlines $ map rowToLine lst
putKuku1 = display table
-- @see http://d.hatena.ne.jp/hekominn/20100902/1283414042
kuku2 :: (Num a) => [a] -> [a] -> [[a]]
kuku2 [] lst = []
kuku2 (x:xs) lst = (map (* x) lst) : kuku2 xs lst
-- main :: IO()
-- main = print $ pretyPrintKuku $ kuku2 [1..5] [1..5]
-- or
-- main = putStr $ pretyPrintKuku $ kuku2 [1..5] [1..5]
-- main = print $ rowToLine1 [1..5]
rowToLine1 :: (Show a) => [a] -> String
rowToLine1 = concatMap (\x -> " " ++ (show x) ++ " ")
pretyPrintKuku :: (Show a) => [[a]] -> String
pretyPrintKuku = unlines . (map rowToLine1)
-- @see http://tnomura9.exblog.jp/10186785/
main = do putStr $ format $ kuku3 [1..9]
kuku3 xs = [map (*x) xs | x <- xs]
format xs = formatD $ map (formatC . formatB) xs
formatA x = reverse $ take 3 $ reverse (" " ++ show x)
formatB xs = map formatA xs
-- unwords:
formatC xs = unwords xs ++ "\n"
formatD xs = foldl (++) [] xs
-- @see http://d.hatena.ne.jp/wvogel00/20110529/1306668936
kuku4 = putStr $ unlines $ map show $ map (kuku' []) [1..9]
where
kuku' xs k = if length xs == 9
then xs
else kuku' (xs ++ [k*(length xs + 1)]) k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment