Sha256: 633e79fbde22fdf5eac7e7932e1a6ec9b491c2a0371c69380a1e7e2a2c3ca052

Contents?: true

Size: 717 Bytes

Versions: 193

Compression:

Stored size: 717 Bytes

Contents

module Spiral (spiral) where

-- | Create a square matrix of natural numbers in a inward, clockwise spiral.
spiral :: Int -> [[Int]]
spiral l = matrix (spiralIndex (l, l)) (l, l)
  where
    -- If an index isn't in the first row of a matrix, it is in the
    -- 90 degress-rotated, complementary matrix.
    spiralIndex :: Integral a => (a, a) -> (a, a) -> a
    spiralIndex     _  (1, j) = j
    spiralIndex (h, w) (i, j) = w + spiralIndex (w, h - 1) (w - j + 1, i - 1)

-- Build a lazy, list-based matrix based on a formula.
matrix :: ((Int, Int) -> a) -> (Int, Int) -> [[a]]
matrix f (rows, columns) = [[ f (i, j)
                            | j <- [1..columns] ]
                            | i <- [1..rows   ] ]

Version data entries

193 entries across 193 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.179 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.178 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.177 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.176 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.175 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.174 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.173 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.172 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.171 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.170 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.169 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.167 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.166 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.165 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.164 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.163 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.162 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.161 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.160 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs