Spiral matrix: Difference between revisions

Content added Content deleted
(Updated to work with Nim 1.4 + removed “^” procedure, removed “newSeqWith”, removed useless type “Pos”)
Line 1,975: Line 1,975:
Or less ambitiously,
Or less ambitiously,
{{Trans|AppleScript}}
{{Trans|AppleScript}}
<lang Haskell>import Data.List (intercalate, transpose)
<lang Haskell>import Control.Monad (join)
import Control.Monad (join)
import Data.List (intercalate, transpose)


spiral :: Int -> [[Int]]
spiral :: Int -> [[Int]]
Line 1,984: Line 1,984:
| 0 < rows =
| 0 < rows =
[x .. pred cols + x] :
[x .. pred cols + x] :
fmap
fmap reverse (transpose $ go cols (pred rows) (x + cols))
reverse
(transpose $ go cols (pred rows) (x + cols))
| otherwise = [[]]
| otherwise = [[]]


--------------------- TABLE FORMATTING -------------------

-- TABLE FORMATTING ----------------------------------------


wikiTable :: Show a => [[a]] -> String
wikiTable :: Show a => [[a]] -> String
wikiTable =
wikiTable =
join .
join
("{| class=\"wikitable\" style=\"text-align: right;" :) .
. ("{| 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
(++ "\n|}") .
. (<> "\n|}")
intercalate "\n|-\n" .
. intercalate "\n|-\n"
fmap (('|' :) . (' ' :) . intercalate " || " . fmap show)</lang>
. fmap (('|' :) . (' ' :) . intercalate " || " . fmap show)</lang>
{{Out}}
{{Out}}
{| class="wikitable" style="text-align: right;width:12em;height:12em;table-layout:fixed;"|-
{| class="wikitable" style="text-align: right;width:12em;height:12em;table-layout:fixed;"|-