RosettaCodeData/Task/Floyds-triangle/Haskell/floyds-triangle-1.hs
2023-07-01 13:44:08 -04:00

32 lines
680 B
Haskell

--------------------- FLOYDS TRIANGLE --------------------
floydTriangle :: [[Int]]
floydTriangle =
( zipWith
(fmap (.) enumFromTo <*> (\a b -> pred (a + b)))
<$> scanl (+) 1
<*> id
)
[1 ..]
--------------------------- TEST -------------------------
main :: IO ()
main = mapM_ (putStrLn . formatFT) [5, 14]
------------------------- DISPLAY ------------------------
formatFT :: Int -> String
formatFT n = unlines $ unwords . zipWith alignR ws <$> t
where
t = take n floydTriangle
ws = length . show <$> last t
alignR :: Int -> Int -> String
alignR n =
( (<>)
=<< flip replicate ' '
. (-) n
. length
)
. show