Multiplication tables: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Switched core type from [Int] to Maybe Int)
Line 2,254: Line 2,254:
Or, making do without imports beyond the Prelude, and separating data from formatting:
Or, making do without imports beyond the Prelude, and separating data from formatting:


<lang Haskell>multTable :: Int -> [[[Int]]]
<lang Haskell>multTable :: Int -> [[Maybe Int]]
multTable n = (\x -> [x] : [] : (
multTable n =
(\y -> if y < x then [] else [x * y])
(\x -> Just x : Nothing : (
<$> range))
(\y -> if y < x then Nothing else Just (x * y))
<$> range
<$> range))
where range = [1..n]
<$> range
where range = [1..n]


tableString :: [[[Int]]] -> String
tableString :: [[Maybe Int]] -> String
tableString tbl = unlines $ (concat . (fmt <$>)) <$> tbl
tableString tbl =
unlines $ (concat . (fmt <$>)) <$> tbl
where
pad = " "
fmt [ ] = pad
fmt [e] = drop (length s) (pad ++ s)
where
where
s = show e
pad = " "
fmt Nothing = pad
fmt (Just n) = drop (length s) (pad ++ s)
where
s = show n


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