Skip to content

Instantly share code, notes, and snippets.

@l1x
Last active May 10, 2020 10:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l1x/9c3b598dd62c8d240050c10fe0947dbf to your computer and use it in GitHub Desktop.
Save l1x/9c3b598dd62c8d240050c10fe0947dbf to your computer and use it in GitHub Desktop.
Spiral print in Haskell
module Main where
transpose :: [[Int]] -> [[Int]]
transpose m =
case m of
[] -> []
([]:_) -> []
x -> (map head x) : transpose (map (drop 1) x)
spiralPrintShort :: [[Int]] -> [Int]
spiralPrintShort matrix =
case matrix of
[] -> []
x:xs -> x ++ spiralPrintShort (reverse (transpose xs))
main :: IO ()
main = do
print (spiralPrintShort [[1, 2, 3], [8, 9, 4], [7, 6, 5]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment