Created
November 14, 2023 06:27
-
-
Save nobsun/5d6207894bc7cb62e0bd5e06df008969 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
module BorderedMatrix where | |
import Data.Array | |
import Data.List | |
{- | | |
>>> mapM_ print sample | |
[1,2,3] | |
[4,5,6] | |
>>> mapM_ print (borderedMatrix 0 sample) | |
[0,0,0,0,0] | |
[0,1,2,3,0] | |
[0,4,5,6,0] | |
[0,0,0,0,0] | |
-} | |
borderedMatrix :: a -> [[a]] -> [[a]] | |
borderedMatrix e | |
= map (bordered e) . transpose . map (bordered e) . transpose | |
bordered :: a -> [a] -> [a] | |
bordered e = (e:) . reverse . (e:) . reverse | |
sample :: [[Int]] | |
sample = [[1,2,3],[4,5,6]] | |
{- | | |
>>> print (matrixWithBorder 0 sample) | |
array ((-1,-1),(2,3)) [((-1,-1),0),((-1,0),0),((-1,1),0),((-1,2),0),((-1,3),0),((0,-1),0),((0,0),1),((0,1),2),((0,2),3),((0,3),0),((1,-1),0),((1,0),4),((1,1),5),((1,2),6),((1,3),0),((2,-1),0),((2,0),0),((2,1),0),((2,2),0),((2,3),0)] | |
-} | |
matrixWithBorder :: a -> [[a]] -> Array (Int, Int) a | |
matrixWithBorder e mat | |
= listArray ((-1,-1), (h,w)) (concat (borderedMatrix e mat)) | |
where | |
h = length mat | |
w = length (head mat) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment