Multiplication tables: Difference between revisions

m
Line 2,814:
table :: [Int] -> [[Maybe Int]]
table xs =
let(Nothing : axis) = Just <$> xs:
zipWith
in (Nothing : axis) :
zipWith(:)
(:)axis
[ [ bool (Just (x * y)) Nothing (x > y)
axis
[ [ bool (Just (x *| y)) Nothing<- (xxs > y)]
| yx <- xs ]
where
axis = | xJust <-$> xs ]
 
-- TEST ------------------------- TEST ---------------------------
main :: IO ()
main =
(putStrLn . unlines) $
(showTable . table) <$> [[13 .. 20], [1 .. 12], [95 .. 100]]
 
-- FORMATTING ---------------------- FORMATTING ------------------------
showTable :: [[Maybe Int]] -> String
showTable xs = unlines $ head rows : [] : tail rows
where
let w = 1 + (length . show) (fromMaybe 0 $ (last . last) xs)
gap = replicate w ' '
rowsgap = (maybe gap (rjustreplicate w ' ' . show) =<<) <$> xs
rows = (maybe gap (rjust w ' ' . show) =<<) <$> xs
in unlines $ head rows : [] : tail rows
 
rjust :: Int -> Char -> String -> String
rjust n c = (drop . length) <*> (replicate n c ++)</lang>
</lang>
{{Out}}
<pre> 13 14 15 16 17 18 19 20
9,659

edits