Multiplication tables: Difference between revisions

Content added Content deleted
(Add task to ARM assembly Raspberry pi)
(→‎{{header|Haskell}}: Abbreviated slightly with bool)
Line 2,580: Line 2,580:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.Monoid ((<>))
<lang haskell>import Data.List (intercalate, transpose)
import Data.List (intercalate, transpose)
import Data.Monoid ((<>))
import Data.Bool (bool)


multTable :: Int -> [[String]]
mulTable :: Int -> [[String]]
multTable n =
mulTable n =
let xs = [1 .. n]
let xs = [1 .. n]
in xs >>=
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 :: String -> [[String]] -> [String]
table delim rows =
table gap rows =
let justifyRight c n s = drop (length s) (replicate n c <> s)
let justifyRight c n s = drop (length s) (replicate n c <> s)
in intercalate delim <$>
in intercalate gap <$>
transpose
transpose
((fmap =<< justifyRight ' ' . maximum . fmap length) <$> transpose rows)
((fmap =<< justifyRight ' ' . maximum . fmap length) <$> transpose rows)


main :: IO ()
main :: IO ()
main = (putStrLn . unlines . table " " . multTable) 12</lang>
main = (putStrLn . unlines . table " " . mulTable) 12</lang>
{{Out}}
{{Out}}
<pre> 1: 1 2 3 4 5 6 7 8 9 10 11 12
<pre> 1: 1 2 3 4 5 6 7 8 9 10 11 12