Multiplication tables: Difference between revisions

Content added Content deleted
Line 2,322: Line 2,322:
<lang haskell>import Data.List (maximumBy)
<lang haskell>import Data.List (maximumBy)
import Data.Ord (comparing)
import Data.Ord (comparing)
import Data.Monoid
import Data.Monoid ((<>))


multTable :: Int -> [[String]]
multTable :: Int -> [[String]]
multTable n =
multTable n =
(\x ->
(\x ->
(show x) <> ":" :
show x <> ":" :
((\y ->
((\y ->
if y < x
if y < x
Line 2,336: Line 2,336:


tableString :: [[String]] -> String
tableString :: [[String]] -> String
tableString tbl =
tableString tbl = unlines $ (unwords . (justifyRight colWidth ' ' <$>)) <$> tbl
unlines $
(unwords . (justifyRight colWidth ' ' <$>)) <$> tbl
where
where
colWidth = length $ (last . last) tbl
colWidth = length $ (last . last) tbl
justifyRight n c s = drop (length s) ((replicate n c) <> s)
justifyRight n c s = drop (length s) (replicate n c <> s)


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