September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
37
Task/Zig-zag-matrix/Haskell/zig-zag-matrix-3.hs
Normal file
37
Task/Zig-zag-matrix/Haskell/zig-zag-matrix-3.hs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import Data.List (mapAccumL)
|
||||
import Data.Text (justifyRight, pack, unpack)
|
||||
|
||||
zigZag :: Int -> [[Int]]
|
||||
zigZag n = horizontals n (diagonals n)
|
||||
where
|
||||
|
||||
diagonals :: Int -> [[Int]]
|
||||
diagonals n =
|
||||
snd $
|
||||
mapAccumL
|
||||
(\xs h ->
|
||||
let (grp, rst) = splitAt h xs
|
||||
in ( rst
|
||||
, (if mod h 2 /= 0
|
||||
then reverse
|
||||
else id)
|
||||
grp))
|
||||
[0 .. (n * n) - 1]
|
||||
(slope ++ [n] ++ reverse slope)
|
||||
where
|
||||
slope = [1 .. n - 1]
|
||||
|
||||
horizontals :: Int -> [[Int]] -> [[Int]]
|
||||
horizontals n xss =
|
||||
if not (null xss)
|
||||
then let (edge, rst) = splitAt n xss
|
||||
in (head <$> edge) :
|
||||
horizontals n (dropWhile null (tail <$> edge) ++ rst)
|
||||
else []
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
putStrLn $
|
||||
unlines $
|
||||
(concat . (unpack <$>)) . ((justifyRight 3 ' ' . pack . show) <$>) <$>
|
||||
zigZag 5
|
||||
Loading…
Add table
Add a link
Reference in a new issue