Multiplication tables: Difference between revisions
→{{header|Haskell}}: Simplified the first version, disentangled model from display, updated output
(→{{header|Haskell}}: Simplified the first version, disentangled model from display, updated output) |
|||
Line 2,580:
=={{header|Haskell}}==
<lang haskell>import Data.
let
in
zipWith
(:)
axis
table :: String -> [[String]] -> [String]▼
(xs >>=
\x ->
let justifyRight c n s = drop (length s) (replicate n c <> s)▼
[ xs >>=
then [Nothing]
else [Just (x * y)]
])
-- TEST ---------------------------------------------------
main :: IO ()
main = do
putStrLn $ showTable $ table [5 .. 10]
putStrLn $ showTable $ table [1 .. 12]
putStrLn $ showTable $ table [95 .. 100]
-- FORMATTING ---------------------------------------------
showTable :: [[Maybe Int]] -> String
showTable xs =
let w = 1 + (length . show) (fromMaybe 0 $ (last . last) xs)
gap = replicate w ' '
rows = (maybe gap (rjust w ' ' . show) =<<) <$> xs
in unlines $ head rows : [] : tail rows
{{Out}}
<pre>
2: 4 6 8 10 12 14 16 18 20 22 24▼
9: 81 90 99 108▼
11: 121 132▼
3 9 12 15 18 21 24 27 30 33 36
4 16 20 24 28 32 36 40 44 48
5 25 30 35 40 45 50 55 60
6 36 42 48 54 60 66 72
7 49 56 63 70 77 84
8 64 72 80 88 96
10 100 110 120
12 144
95 96 97 98 99 100
95 9025 9120 9215 9310 9405 9500
96 9216 9312 9408 9504 9600
97 9409 9506 9603 9700
98 9604 9702 9800
99 9801 9900
100 10000</pre>
Or, more roughly and directly:
|