SHARE
TWEET

Untitled

Spiritreader Dec 5th, 2019 141 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. diag :: [[a]] -> [a]
  2. diag mat = hstep mat 0 []
  3.     where
  4.         hstep :: [[a]] -> Int -> [a] -> [a]
  5.         hstep [] _ _ = []
  6.         hstep ls@(h:rls) col accu = case (h !!! col) of
  7.                                         Just _ -> accu ++ hstep ls (col+1) (diagonal ls col [])
  8.                                         Nothing -> accu ++ vstep rls (col-1)
  9.                                    
  10.         vstep :: [[a]] -> Int -> [a]
  11.         vstep [] _ = []
  12.         vstep ls@(_:rls) col = diagonal ls col [] ++ (vstep rls col)
  13.  
  14.         diagonal :: [[a]] -> Int -> [a] -> [a]
  15.         diagonal [] _ out = reverse out
  16.         diagonal (x:xs) col out = if col >= 0 then diagonal xs (col-1) ((x !! col):out) else reverse out
  17.  
  18. (!!!) :: [a] -> Int -> Maybe a
  19. (!!!) _  n | n < 0 = Nothing
  20. (!!!) []     _     = Nothing
  21. (!!!) (x:_)  0     = Just x
  22. (!!!) (_:xs) n     = (!!!) xs (n - 1)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top