Multiplication tables: Difference between revisions

→‎{{header|Haskell}}: Switched core type from [Int] to Maybe Int
(→‎{{header|Haskell}}: Switched core type from [Int] to Maybe Int)
Line 2,254:
Or, making do without imports beyond the Prelude, and separating data from formatting:
 
<lang Haskell>multTable :: Int -> [[[Maybe Int]]]
multTable n = (\x -> [x] : [] : (
(\yx -> if y <Just x then [] else [x: *Nothing y]): (
(\y -> if y < x then Nothing else Just (x * <$> rangey))
<$> range))
where <$> range = [1..n]
where range = [1..n]
 
tableString :: [[[Maybe Int]]] -> String
tableString tbl = unlines $ (concat . (fmt <$>)) <$> tbl
unlines $ (concat . (fmt <$>)) <$> tbl
where
pad = " "
fmt [ ] = pad
fmt [e] = drop (length s) (pad ++ s)
where
pad s = show" e"
fmt [ ]Nothing = pad
fmt [e](Just n) = drop (length s) (pad ++ s)
where
pad = " s = show "n
 
main :: IO ()
9,659

edits