Multiplication tables: Difference between revisions

→‎{{header|Haskell}}: Abbreviated slightly with bool
(Add task to ARM assembly Raspberry pi)
(→‎{{header|Haskell}}: Abbreviated slightly with bool)
Line 2,580:
 
=={{header|Haskell}}==
<lang haskell>import Data.MonoidList ((<>)intercalate, transpose)
import Data.ListMonoid (intercalate, transpose(<>))
import Data.Bool (bool)
 
multTablemulTable :: Int -> [[String]]
multTablemulTable n =
let xs = [1 .. n]
in xs >>=
\x -> [show x <> ":" : (xs >>= \y -> [bool (show (x * y)) mempty (y < x)])]
\x ->
[ show x <> ":" :
(xs >>=
\y ->
[ if y < x
then mempty
else show (x * y)
])
]
 
table :: String -> [[String]] -> [String]
table delimgap rows =
let justifyRight c n s = drop (length s) (replicate n c <> s)
in intercalate delimgap <$>
transpose
((fmap =<< justifyRight ' ' . maximum . fmap length) <$> transpose rows)
 
main :: IO ()
main = (putStrLn . unlines . table " " . multTablemulTable) 12</lang>
{{Out}}
<pre> 1: 1 2 3 4 5 6 7 8 9 10 11 12
9,658

edits