我正在尝试从接收到一个始终从0开始的计数器Int(i)、另一个Int(n)作为每一行长度以及要转换为矩阵的列表中将列表转换为数组(列表的列表)的函数中进行转换。
这将输出以下内容:
当它应该是这样的:
createMatrix :: (Int, Int, [Int]) -> [[Int]]
createMatrix (i, n, ([])) = []
createMatrix (i, n, (x:y)) = if (i < n) then [x] : createMatrix (i+1, n, (y))
else [] ++ createMatrix (0, n, (x:y))
这将输出以下内容:
[[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18]]
当它应该是这样的:
[[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18]]
如果n等于6
有没有人有什么想法,可能是我做错了什么?我尝试了几种方法,但没有找到解决方案。
谢谢
createMatrix n xs = take n xs : createMatrix n (drop n xs)
这样就足够了。您不需要两个计数器;另一个计数器是由take
和drop
内部维护的。 - chepnercreateMatrix _ [] = []
,然后是createMatrix n xs = take n xs : createMatrix n (drop n xs)
。 - user855443