Created
June 2, 2019 20:47
-
-
Save morteako/5c8a21ffd4e4d03be4f0bb58d181233e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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