1
我目前正在为我的类进行分配,其中一个要求是创建一个名为rotate90的函数。该功能基本上带有[[Char]]并顺时针旋转90度。旋转Haskell中的char矩阵图像
例如:
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”。 如何让它打印剩下的部分?
非常感谢! –
哦,等一下,有没有使用转置的另一种方法吗?除地图和反向之外,我们不应该使用任何内置函数。 –
你总是可以自己定义'转置',它只依赖于模式匹配和递归。 [代码可以在这里找到。](http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/Data-List.html#transpose) – David