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.139 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.138 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.137 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.136 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.135 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.134 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.133 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.132 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.131 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.130 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.129 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.128 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.127 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.126 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.125 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.124 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.123 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.122 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.121 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs
trackler-2.2.1.120 tracks/haskell/exercises/spiral-matrix/examples/success-standard/src/Spiral.hs