我需要在Haskell中迭代一个矩阵,我不知道该怎么做。
我在.txt
文件中有我的矩阵所以我得到了这一行的内容:contents <- readFile file
,然后我需要逐个迭代该矩阵,因为我需要将值转换为一个JValue。
这是我的代码:
myFunction file = do
contents <- readFile file
let element = endBy "," contents
print(element)
我该怎么办?
答案 0 :(得分:-1)
我不知道你想如何表示矩阵,但这是我的解决方案:
import System.IO
convLine::String -> [String]
convLine (x:[]) = ["b"]
convLine (x:xs) = ["a"] ++ convLine xs
convMatrix::[String] -> [[String]]
convMatrix (x:[]) = [convLine x]
convMatrix (x:xs) = [convLine x] ++ convMatrix xs
main = do
handler <- openFile "./matrix" ReadMode
content <- hGetContents handler
let input = lines content
print $ convMatrix input
我输入文件的内容./matrix是
123
123
123
我的结果是:
[[ “一”, “一个”, “B”],[ “一”, “一个”, “B”],[ “一”, “一个”, “B”]]
我刚刚展示了如何在某种矩阵表示中转换值的示例。