Multiplication tables: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: Switched core type from [Int] to Maybe Int) |
m (→{{header|Haskell}}: (Layout)) |
||
Line 2,242: | Line 2,242: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<lang haskell> |
<lang haskell>multTable :: Int -> [[Maybe Int]] |
||
⚫ | |||
import Text.Printf |
|||
(\x -> |
|||
⚫ | |||
main = do |
|||
⚫ | |||
putStrLn $ " x" ++ concatMap fmt [1..12] |
|||
((\y -> |
|||
zipWithM_ f [1..12] $ iterate (" " ++) "" |
|||
⚫ | |||
where f n s = putStrLn $ fmt n ++ s ++ concatMap (fmt . (*n)) [n..12] |
|||
then Nothing |
|||
⚫ | |||
⚫ | |||
⚫ | |||
Or, making do without imports beyond the Prelude, and separating data from formatting: |
|||
⚫ | |||
range = [1 .. n] |
|||
<lang Haskell>multTable :: Int -> [[Maybe Int]] |
|||
⚫ | |||
(\x -> Just x : Nothing : ( |
|||
⚫ | |||
<$> range)) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
tableString :: [[Maybe Int]] -> String |
tableString :: [[Maybe Int]] -> String |
||
tableString tbl = |
tableString tbl = unlines $ (concat . (fmt <$>)) <$> tbl |
||
where |
|||
unlines $ (concat . (fmt <$>)) <$> tbl |
|||
pad = " " |
|||
fmt Nothing = pad |
|||
⚫ | |||
where |
where |
||
s = show n |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
main :: IO () |
main :: IO () |