Skip to content

Instantly share code, notes, and snippets.

@l1x

l1x/Spiral.hs

Last active May 10, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.