小编Emm*_*Lee的帖子

仅使用 `!!` 和 `length` 实现矩阵的转置

我正在努力确定如何在 Haskell 中仅使用!!和对矩阵进行转置length。我已经编写了一个函数来获取任何给定索引处的矩阵列i

getCol :: [[Int]] -> Int -> [Int] 
getCol [] i = []
getCol (xs:xss) i = if isMatrix xss then (xs !! i) : getCol xss i else []
Run Code Online (Sandbox Code Playgroud)

另外,还有一个返回行长度的函数:

rowLength :: [[Int]] -> Int
rowLength (x:xs) = length x
Run Code Online (Sandbox Code Playgroud)

我现在的计划是n用 的条目填充列表时间getColn作为rowLength我调用该函数的列表的 。

因此,对于此示例列表:

list = [[1,2,3],[4,5,6],[7,8,9]]

-- rowLength ---> 3
-- then fill another list with every column of list, 
--     for …
Run Code Online (Sandbox Code Playgroud)

haskell transpose functional-programming list

5
推荐指数
1
解决办法
215
查看次数

标签 统计

functional-programming ×1

haskell ×1

list ×1

transpose ×1