Haskell 从带索引的字符串矩阵递归地将数据元素添加到矩阵
Haskell recursively adding data elements to matrix from string matrix with indiсes
请跳转到编辑:
我的作业中有一个问题,也许我完全错了,但我是函数式编程的新手。
我有一个包含字符串的 5x5 矩阵,我想读出它们,将它们转换为数据元素并将其应用于新矩阵。
我正在使用递归方法,因为我想将字符串的位置添加到数据元素中!
这是我尝试过的:
cycleMatrix :: Int -> Int -> [[String]] -> [[Data]]
cycleMatrix 0 0 matrix = (strToData 0 0 (matrix !! 0) !! 0))
cycleMatrix n 0 matrix = (cycleMatrix (n - 1) 0 matrix):(strToData n 5 ((matrix !! n) !! 5))
cycleMatrix n m matrix = (cycleMatrix n (m - 1) matrix):(strToData n n ((matrix !! n) !! m))
使用 strToData
我从矩阵中获取字符串并传递 x 和 y 返回一个数据对象(正常工作)
目前我只是想将每个元素添加到一个数组中,但我真的想在每次 m
(cycleMatrix n 0 matrix
之后)归零时开始一个新列表
我的问题是:这是要走的路吗?如何以正确的方式附加此元素?
编辑:好吧,我终于通过拆分函数让它工作了(感谢 Rudi 告诉我不要一次做所有事情)
但我的问题仍然存在。我现在有 8 个包含数据元素的列表
我可以使用 [] ++ []
将它们全部附加到列表中,但是有没有办法从列表中创建一个矩阵,例如 [] : []
(将列表作为元素添加到列表中)?
我会通过将内部列表枚举成 (x, data) 元组¹,然后将此枚举枚举成 (y, [x-lists]) 来解决这个问题。这是将其转换为所需格式的起点。
您可以使用 :load file.hs
在 ghci 中加载此文件,并在那里使用不同的功能。
¹根据您的编辑,我猜这就是您已经做过的
example :: [[String]]
example = [
["as", "df", "ghj"],
["xx", "yy"]
]
-- add a position number to each list element. Beware
-- that this starts with 1
enumerate :: [a] -> [(Int, a)]
enumerate = zip [1..]
-- does the enumeration, but does not transform the
-- data into the desired format. I put it here, so that
-- the functionality of the different functions is
-- better to understand.
kindofEnumarate2d :: [[a]] -> [(Int, [(Int, a)])]
kindofEnumarate2d = enumerate . map enumerate
-- convert a row of (y, [(x, value)]) tuples into a
-- [((x,y), value)] list
helper :: (Int, [(Int, a)]) -> [((Int, Int), a)]
helper (y, xs) = [((x, y), s) | (x, s) <- xs]
-- transform each row of the "kind-of" transformed
-- rows into the desired format.
enumerate2d :: [[a]] -> [[((Int, Int), a)]]
enumerate2d = map helper . kindofEnumarate2d
请跳转到编辑:
我的作业中有一个问题,也许我完全错了,但我是函数式编程的新手。
我有一个包含字符串的 5x5 矩阵,我想读出它们,将它们转换为数据元素并将其应用于新矩阵。
我正在使用递归方法,因为我想将字符串的位置添加到数据元素中!
这是我尝试过的:
cycleMatrix :: Int -> Int -> [[String]] -> [[Data]]
cycleMatrix 0 0 matrix = (strToData 0 0 (matrix !! 0) !! 0))
cycleMatrix n 0 matrix = (cycleMatrix (n - 1) 0 matrix):(strToData n 5 ((matrix !! n) !! 5))
cycleMatrix n m matrix = (cycleMatrix n (m - 1) matrix):(strToData n n ((matrix !! n) !! m))
使用 strToData
我从矩阵中获取字符串并传递 x 和 y 返回一个数据对象(正常工作)
目前我只是想将每个元素添加到一个数组中,但我真的想在每次 m
(cycleMatrix n 0 matrix
之后)归零时开始一个新列表
我的问题是:这是要走的路吗?如何以正确的方式附加此元素?
编辑:好吧,我终于通过拆分函数让它工作了(感谢 Rudi 告诉我不要一次做所有事情)
但我的问题仍然存在。我现在有 8 个包含数据元素的列表
我可以使用 [] ++ []
将它们全部附加到列表中,但是有没有办法从列表中创建一个矩阵,例如 [] : []
(将列表作为元素添加到列表中)?
我会通过将内部列表枚举成 (x, data) 元组¹,然后将此枚举枚举成 (y, [x-lists]) 来解决这个问题。这是将其转换为所需格式的起点。
您可以使用 :load file.hs
在 ghci 中加载此文件,并在那里使用不同的功能。
¹根据您的编辑,我猜这就是您已经做过的
example :: [[String]]
example = [
["as", "df", "ghj"],
["xx", "yy"]
]
-- add a position number to each list element. Beware
-- that this starts with 1
enumerate :: [a] -> [(Int, a)]
enumerate = zip [1..]
-- does the enumeration, but does not transform the
-- data into the desired format. I put it here, so that
-- the functionality of the different functions is
-- better to understand.
kindofEnumarate2d :: [[a]] -> [(Int, [(Int, a)])]
kindofEnumarate2d = enumerate . map enumerate
-- convert a row of (y, [(x, value)]) tuples into a
-- [((x,y), value)] list
helper :: (Int, [(Int, a)]) -> [((Int, Int), a)]
helper (y, xs) = [((x, y), s) | (x, s) <- xs]
-- transform each row of the "kind-of" transformed
-- rows into the desired format.
enumerate2d :: [[a]] -> [[((Int, Int), a)]]
enumerate2d = map helper . kindofEnumarate2d