Multiplication tables: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Adjusted for more flexible column widths (for larger tables and longer integer strings))
(→‎{{header|Haskell}}: Simpler route to max integer string length)
Line 2,326: Line 2,326:
multTable n =
multTable n =
(\x ->
(\x ->
mappend (show x) ":" :
(mappend (show x) ":") :
((\y ->
((\y ->
if y < x
if y < x
Line 2,333: Line 2,333:
[1 .. n])) <$>
[1 .. n])) <$>
[1 .. n]
[1 .. n]

tableString :: Int -> String
tableString :: Int -> String
tableString n = unlines $ (unwords . (justifyRight (maxLen tbl) ' ' <$>)) <$> tbl
tableString n = unlines $ (unwords . ((justifyRight w ' ') <$>)) <$> tbl
where
where
justifyRight n c s = drop (length s) (mappend (replicate n c) s)
mx = maximumBy (comparing length)
maxLen = length . mx . (mx <$>)
tbl = multTable n
tbl = multTable n
w = length $ (last . last) tbl
justifyRight n c s = drop (length s) (mappend (replicate n c) s)


main :: IO ()
main :: IO ()