我目前正在为我的类(class)做作业,其中一项要求是创建一个名为 rotate90 的函数。此函数基本上接受一个 [[Char]] 并将其顺时针旋转 90 度。
例如:
type Picture = [[Char]]
pic :: Picture
pic = [ "123",
"456",
"789" ]
变成:
[ "741",
"852",
"963" ]
到目前为止,我的代码看起来像这样:
rotate90 :: Picture -> Picture
rotate90 (x:xs)
| (x:xs) == [] = []
| xs == [] && x /= [] = formRow ([[]]) (formCol x)
| xs /= [] = formRow (rotate90 xs) (formCol x)
formCol :: [Char] -> [[Char]]
formCol y = [[a] | a <- y]
formRow :: [[Char]] -> [[Char]] -> [[Char]]
formRow (x:xs) (y:ys)
| xs == [] || ys == [] = (x++y):[]
| otherwise = (x++y):formRow xs ys
现在它只打印矩阵的第一“行”,在示例中是“741”。 我如何让它打印其余部分?
最佳答案
关于 Data.List.transpose
的简单实现是
-- | Rotate clockwise
cw = map reverse . transpose
-- | Rotate counter-clockwise
cw = reverse . transpose
转置原始图片产量
147
258
369
并且反转每一行导致旋转图片
741
852
963
一般来说,您可以使用以下三个函数的组合来表达任意方向的镜像和旋转:
transpose
map reverse -- mirror left <-> right
reverse -- mirror top <-> bottom
关于list - 在 Haskell 中旋转 char 矩阵图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14686731/