Jump to content

Multiplication tables: Difference between revisions

m
→‎{{header|Haskell}}: Tidied, removed one import.
m (→‎{{header|Haskell}}: Tidied, removed one import.)
Line 3,517:
=={{header|Haskell}}==
<lang haskell>import Data.Maybe (fromMaybe, maybe)
import Data.Bool (bool)
 
--------------------------- TESTMULTIPLICATION TABLE ---------------------------
table :: [Int] -> [[Maybe Int]]
 
table xs =
tablemulTable :: [Int] -> [[Maybe Int]]
mulTable xs =
(Nothing : axis) :
zipWith
(:)
axis
[ [upperMul bool (Just (x *y | y)) Nothing<- xs] | (x ><- y)xs]
| y <- xs ]
| x <- xs ]
where
axis = Just <$> xs
upperMul x y
| x <-> y xs= ]Nothing
| otherwise = Just (x * y)
 
 
--------------------------- TEST ---------------------------
--------------------------- TEST -------------------------
main :: IO ()
main =
(putStrLn . unlines) $
showTable . mulTable
showTable . table <$> [[13 .. 20], [1 .. 12], [95 .. 100]]
<$> [ [13 .. 20],
[1 .. 12],
[95 .. 100]
| y <- xs ]
 
------------------------ FORMATTING ------------------------
showTable :: [[Maybe Int]] -> String
showTable xs = unlines $ head rows : [] : tail rows
Line 3,544 ⟶ 3,551:
gap = replicate w ' '
rows = (maybe gap (rjust w ' ' . show) =<<) <$> xs
rjust n c = (drop . length) <*> (replicate n c ++<>)</lang>
 
rjust :: Int -> Char -> String -> String
rjust n c = (drop . length) <*> (replicate n c ++)</lang>
{{Out}}
<pre> 13 14 15 16 17 18 19 20
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.