RosettaCodeData/Task/Zig-zag-matrix/Haskell/zig-zag-matrix-1.hs
2019-09-12 10:33:56 -07:00

14 lines
352 B
Haskell

import Data.Array (array, bounds, range, (!))
import Data.Monoid (mappend)
import Data.List (sortBy)
import Text.Printf (printf)
compZig (x, y) (x', y') =
compare (x + y) (x' + y') `mappend`
if even (x + y)
then compare x x'
else compare y y'
zigZag upper = array b $ zip (sortBy compZig (range b)) [0 ..]
where
b = ((0, 0), upper)