Skip to content

Instantly share code, notes, and snippets.

@morteako
Created June 2, 2019 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morteako/5c8a21ffd4e4d03be4f0bb58d181233e to your computer and use it in GitHub Desktop.
Save morteako/5c8a21ffd4e4d03be4f0bb58d181233e to your computer and use it in GitHub Desktop.
import Control.Monad ((<=<))
import Data.Maybe (catMaybes, listToMaybe)
import Safe
xss = [
[1,2,3],
[4,5,6],
[7,8,9]
]
xss' = [
[1,2,3,3],
[4,5,6,6],
[7,8,9,9]
]
xss2 = [
[1,2,3],
[4,5,6],
[7,8,9],
[70,80,90]
]
getDiag xs = catMaybes $ zipWith ($) getters xs
where
getters = iterate (<=< tailMay) listToMaybe
getAllSomeDir lifter xss@(xs:_) = [lifter f xss | f <- getters]
where
getters = zipWith const (iterate (. tail) id) xss
getAllOneWay :: [[a]] -> [[[a]]]
getAllOneWay xss = do
rightCombs <- getAllSomeDir fmap xss
allCombs <- getAllSomeDir id rightCombs
return allCombs
getAllBothWays xss = getAllOneWay xss ++ getAllOneWay reved
where
reved = fmap reverse xss
getAllDiagonals = fmap getDiag . getAllBothWays
main = do
putStrLn "\n"
mapM_ print $ getAllDiagonals xss
putStrLn "\n"
mapM_ print $ getAllDiagonals xss2
putStrLn "\n"
mapM_ print $ getAllDiagonals xss'
-- [ | _ <- head xss, ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment