Faulhaber's triangle: Difference between revisions
→{{header|Haskell}}: Tidied, replaced an Arrow with an Applicative.
(→{{header|Haskell}}: Tidied, replaced an Arrow with an Applicative.) |
|||
Line 945:
=={{header|Haskell}}==
{{works with|GHC|
<lang haskell>import Data.Ratio (Ratio, numerator, denominator, (%))
-- Infinite list of rows of Faulhaber's triangle
faulhaberTriangle :: [[Rational]]
Line 965:
faulhaber p n = sum (zipWith ((*) . (n ^)) [1 ..] (faulhaberTriangle !! p))
-- DISPLAY ---------------------------------------------------------------------▼
-- (Max numerator+denominator widths) -> Column width -> Filler -> Ratio -> String▼
justifyRatio :: (Int, Int) -> Int -> Char -> Rational -> String▼
justifyRatio (wn, wd) n c nd =▼
w = max n (wn + wd + 2) -- Minimum column width, or more if specified.▼
in if 1 == den▼
then center w c (show num)▼
else let (q, r) = quotRem (w - 1) 2▼
in concat▼
[ justifyRight q c (show num)▼
, "/"▼
, justifyLeft (q + r) c (show den)▼
]▼
center, justifyLeft, justifyRight :: Int -> Char -> String -> String▼
center n c s =▼
let (q, r) = quotRem (n - length s) 2▼
in concat [replicate q c, s, replicate (q + r) c]▼
justifyLeft n c s = take n (s ++ replicate n c)▼
justifyRight n c s = drop (length s) (replicate n c ++ s)▼
-- List of Ratios -> (Max numerator width, Max denominator width)▼
maxWidths :: [[Rational]] -> (Int, Int)▼
maxWidths xss =▼
let widest f xs = maximum $ fmap (length . show . f) xs▼
in widest numerator &&& widest denominator $ concat xss▼
-- TEST ------------------------------------------------------------------------▼
main :: IO ()
main = do
Line 1,004 ⟶ 976:
[ unlines ((justifyRatio widths 8 ' ' =<<) <$> triangle)
, (show . numerator) (faulhaber 17 1000)
]
▲-- (Max numerator+denominator widths) -> Column width -> Filler -> Ratio -> String
▲justifyRatio :: (Int, Int) -> Int -> Char -> Rational -> String
▲justifyRatio (wn, wd) n c nd = go $ [numerator, denominator] <*> [nd]
where
| otherwise =
▲center n c s =
▲ let (q, r) = quotRem (n - length s) 2
pad = replicate q c
▲-- List of Ratios -> (Max numerator width, Max denominator width)
▲maxWidths :: [[Rational]] -> (Int, Int)
▲maxWidths xss =
▲ let widest f xs = maximum $ fmap (length . show . f) xs
{{Out}}
<pre> 1
|