Zig-zag matrix: Difference between revisions

→‎{{header|Haskell}}: Pruned a (now) redundant import, added signatures, tweaked name to avoid wiki glitch, preferred guards to if then.
m (→‎{{header|Haskell}}: Tidied mapAccumL version)
(→‎{{header|Haskell}}: Pruned a (now) redundant import, added signatures, tweaked name to avoid wiki glitch, preferred guards to if then.)
Line 2,149:
Computing the array:
 
<lang haskell>import Data.Array (Array, array, bounds, range, (!))
import Data.Monoid (mappend)
import Data.List (sortBy)
import Text.Printf (printf)
import Data.MonoidList (mappendsortBy)
 
compZig :: (xInt, yInt) -> (x'Int, y'Int) =-> Ordering
compZig (x, y) (x_, y_) = compare (x + y) (x'x_ + y'y_) `mappend`<> go x y
where
if even (x + y)
then comparego x x'y
if | even (x + y) = compare x x_
else | otherwise = compare y y'y_
 
zigZag :: (Int, Int) -> Array (Int, Int) Int
zigZag upper = array b $ zip (sortBy compZig (range b)) [0 ..]
where
9,655

edits