Spiral matrix: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: (tidying - switched if to guards))
Line 1,884: Line 1,884:
spiral n = go n n 0
spiral n = go n n 0
where
where
go rows cols start =
go rows cols x
if 0 < rows
| 0 < rows =
then [start .. start + pred cols] :
[x .. pred cols + x] :
fmap reverse (transpose $ go cols (pred rows) (start + cols))
fmap reverse (transpose $ go cols (pred rows) (x + cols))
else [[]]
| otherwise = [[]]

main :: IO ()
main = putStrLn $ wikiTable (spiral 5)




Line 1,899: Line 1,896:
wikiTable =
wikiTable =
join .
join .
("{| class=\"wikitable\" style=\"text-align:center;" :) .
("{| class=\"wikitable\" style=\"text-align: right;" :) .
("width:12em;height:12em;table-layout:fixed;\"\n|-\n" :) .
("width:12em;height:12em;table-layout:fixed;\"\n|-\n" :) .
return .
return .
Line 1,906: Line 1,903:
fmap (('|' :) . (' ' :) . intercalate " || " . fmap show)</lang>
fmap (('|' :) . (' ' :) . intercalate " || " . fmap show)</lang>
{{Out}}
{{Out}}
{| class="wikitable" style="text-align:center;width:12em;height:12em;table-layout:fixed;"|-
{| class="wikitable" style="text-align: right;width:12em;height:12em;table-layout:fixed;"|-
| 0 || 1 || 2 || 3 || 4
| 0 || 1 || 2 || 3 || 4
|-
|-