Multiplication tables: Difference between revisions
m
→{{header|Haskell}}: Tidied, removed one import.
m (→{{header|Haskell}}: Tidied, removed one import.) |
|||
Line 3,517:
=={{header|Haskell}}==
<lang haskell>import Data.Maybe (fromMaybe, maybe)
table :: [Int] -> [[Maybe Int]]▼
mulTable xs =
(Nothing : axis) :
zipWith
(:)
axis
[
| y <- xs ]▼
| x <- xs ]▼
where
axis = Just <$> xs
upperMul x y
| otherwise = Just (x * y)
▲--------------------------- TEST ---------------------------
--------------------------- TEST -------------------------
main :: IO ()
main =
(putStrLn . unlines) $
showTable . mulTable
<$> [ [13 .. 20],
[1 .. 12],
[95 .. 100]
------------------------ FORMATTING
showTable :: [[Maybe Int]] -> String
showTable xs = unlines $ head rows : [] : tail rows
Line 3,544 ⟶ 3,551:
gap = replicate w ' '
rows = (maybe gap (rjust w ' ' . show) =<<) <$> xs
▲rjust n c = (drop . length) <*> (replicate n c ++)</lang>
{{Out}}
<pre> 13 14 15 16 17 18 19 20
|