Multiplication tables: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: Tidied, removed one import.)
Line 3,522: Line 3,522:
mulTable :: [Int] -> [[Maybe Int]]
mulTable :: [Int] -> [[Maybe Int]]
mulTable xs =
mulTable xs =
(Nothing : axis) :
(Nothing : labels) :
zipWith
zipWith
(:)
(:)
axis
labels
[[upperMul x y | y <- xs] | x <- xs]
[[upperMul x y | y <- xs] | x <- xs]
where
where
axis = Just <$> xs
labels = Just <$> xs
upperMul x y
upperMul x y
| x > y = Nothing
| x > y = Nothing
Line 3,537: Line 3,537:
main :: IO ()
main :: IO ()
main =
main =
(putStrLn . unlines) $
putStrLn . unlines $
showTable . mulTable
showTable . mulTable
<$> [ [13 .. 20],
<$> [ [13 .. 20],
Line 3,548: Line 3,548:
showTable xs = unlines $ head rows : [] : tail rows
showTable xs = unlines $ head rows : [] : tail rows
where
where
w = 1 + (length . show) (fromMaybe 0 $ (last . last) xs)
w = succ $ (length . show) (fromMaybe 0 $ (last . last) xs)
gap = replicate w ' '
gap = replicate w ' '
rows = (maybe gap (rjust w ' ' . show) =<<) <$> xs
rows = (maybe gap (rjust w ' ' . show) =<<) <$> xs