Floyd's triangle: Difference between revisions

→‎{{header|Haskell}}: Or, delegating the recursion scheme to mapAccumL:
(→‎{{header|Haskell}}: Or, delegating the recursion scheme to mapAccumL:)
Line 1,133:
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105</lang>
 
 
Or, delegating the recursion scheme to mapAccumL:
 
<lang haskell>import Data.List (mapAccumL)
 
floyd :: Int -> [[Int]]
floyd n =
snd $
mapAccumL
(\start row -> (start + row + 1, [start .. start + row]))
1
[0 .. (n - 1)]
 
showFloyd :: [[Int]] -> String
showFloyd xs = unlines $ concatMap (justifyRight w ' ' . show) <$> xs
where
justifyRight n c s = drop (length s) (replicate n c ++ s)
w = (length . show . last . last) xs + 1
 
main :: IO ()
main = mapM_ putStrLn $ (showFloyd . floyd) <$> [5, 14]</lang>
{{Out}}
<pre> 1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
 
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
9,659

edits