Zig-zag matrix: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: Tidied mapAccumL version)
Line 2,193: Line 2,193:


zigZag :: Int -> [[Int]]
zigZag :: Int -> [[Int]]
zigZag = horizontals <*> diagonals
zigZag = go <*> diagonals
where
where
diagonals n =
go _ [] = []
go n xss = (head <$> edge) : go n (dropWhile null (tail <$> edge) <> rst)
let slope = [1 .. n - 1]
in snd $
where
mapAccumL
(edge, rst) = splitAt n xss

(\xs h ->
diagonals :: Int -> [[Int]]
let (grp, rst) = splitAt h xs
diagonals n =
in (rst, bool id reverse (0 /= mod h 2) grp))
[0 .. (n * n) - 1]
snd $ mapAccumL go [0 .. (n * n) - 1] (slope <> [n] <> reverse slope)
where
(slope ++ [n] ++ reverse slope)
horizontals n xss
slope = [1 .. n - 1]
go xs h = (rst, bool id reverse (0 /= mod h 2) grp)
| null xss = []
| otherwise =
where
let (edge, rst) = splitAt n xss
(grp, rst) = splitAt h xs
in (head <$> edge) :
horizontals n (dropWhile null (tail <$> edge) ++ rst)


main :: IO ()
main :: IO ()