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