读取Haskell中的值和列表列表(Read values and list of lists in Haskell)

在将此问题标记为重复之前,我已经阅读了这个主题: Haskell从文件读取整数和列表列表并且解决方案无法解决我的问题。

我正在尝试读取包含此结构的文件中的内容:

String, String, [(Int, Int, Int)]

该文件看起来像这样:

Name1 22/05/2018 [(1, 5, 10), (2, 5, 5), (3, 10, 40)] Name2 23/05/2018 [(1, 10, 10), (2, 15, 5), (3, 50, 40),(4,20,5)] Name3 22/05/2018 [(4, 2, 1), (5, 2, 2), (6, 50, 3), (1,2,3)] Name4 23/05/2018 [(1, 3, 10), (2, 1, 5), (3, 2, 40), (6,20,20)]

在Haskell中,我创建了这个函数来读取文件的内容并将此内容“转换”为我的自定义类型。

rlist :: String -> [(Int, Int, Int)] rlist = read loadPurchases :: IO [(String, String, [(Int, Int, Int)])] loadPurchases = do s <- readFile "tes.txt" return (glpurch (map words (lines s))) glpurch :: [[String]] -> [(String, String, [(Int, Int, Int)])] glpurch [] = [] gplpurch ([name, dt, c]:r) = (name, dt, (rlist c)) : gplpurch r

但是当我尝试执行“loadPurchases”函数时,我得到了这个错误:函数glpurch中的非详尽模式。

使用:set -Wall,我收到了这条帮助信息:

<interactive>:6:1: warning: [-Wincomplete-patterns] Pattern match(es) are non-exhaustive In an equation for `glpurch': Patterns not matched: ([]:_:_) ([_]:_) ([_, _]:_) ((_:_:_:_:_):_)

我的问题是如何创建所有这些条件。

如果有人能帮助我创造那些可能决定“停止条件”的条件,我将非常感激

Before to mark this question as duplicated, I already read this topic: Haskell read Integer and list of lists from file and the solution doesn't solve my problem.

I'm trying to read the content in a File that contains this structure:

String, String, [(Int, Int, Int)]

The file looks something like this:

Name1 22/05/2018 [(1, 5, 10), (2, 5, 5), (3, 10, 40)] Name2 23/05/2018 [(1, 10, 10), (2, 15, 5), (3, 50, 40),(4,20,5)] Name3 22/05/2018 [(4, 2, 1), (5, 2, 2), (6, 50, 3), (1,2,3)] Name4 23/05/2018 [(1, 3, 10), (2, 1, 5), (3, 2, 40), (6,20,20)]

In Haskell, I created this function to read the contents of the file and "convert" this content to my custom type.

rlist :: String -> [(Int, Int, Int)] rlist = read loadPurchases :: IO [(String, String, [(Int, Int, Int)])] loadPurchases = do s <- readFile "tes.txt" return (glpurch (map words (lines s))) glpurch :: [[String]] -> [(String, String, [(Int, Int, Int)])] glpurch [] = [] gplpurch ([name, dt, c]:r) = (name, dt, (rlist c)) : gplpurch r

But when I try to execute the "loadPurchases" function, I get this error: Non-exhaustive patterns in function glpurch.

Using :set -Wall, I received this help message:

<interactive>:6:1: warning: [-Wincomplete-patterns] Pattern match(es) are non-exhaustive In an equation for `glpurch': Patterns not matched: ([]:_:_) ([_]:_) ([_, _]:_) ((_:_:_:_:_):_)

My problem is how to create all these conditions.

I will be very grateful if anyone can help me create those conditions that are likely to determine the "stopping condition"

最满意答案

您只匹配长度为3的列表,实际上每行上有更多的单词。 只需在GHCi中尝试:

> words "Name1 22/05/2018 [(1, 5, 10), (2, 5, 5), (3, 10, 40)]" ["Name1","22/05/2018","[(1,","5,","10),","(2,","5,","5),","(3,","10,","40)]"]

你可能想重新组合前两个词的所有单词:

glpurch ((name : dt : rest) :r) = (name, dt, (rlist $ unwords rest)) : gplpurch r

You are only matching lists of length 3 when in fact there are many more words on each line. Just try it in GHCi:

> words "Name1 22/05/2018 [(1, 5, 10), (2, 5, 5), (3, 10, 40)]" ["Name1","22/05/2018","[(1,","5,","10),","(2,","5,","5),","(3,","10,","40)]"]

You probably want to recombine all words past the first two:

glpurch ((name : dt : rest) :r) = (name, dt, (rlist $ unwords rest)) : gplpurch r读取Haskell中的值和列表列表(Read values and list of lists in Haskell)

在将此问题标记为重复之前,我已经阅读了这个主题: Haskell从文件读取整数和列表列表并且解决方案无法解决我的问题。

我正在尝试读取包含此结构的文件中的内容:

String, String, [(Int, Int, Int)]

该文件看起来像这样:

Name1 22/05/2018 [(1, 5, 10), (2, 5, 5), (3, 10, 40)] Name2 23/05/2018 [(1, 10, 10), (2, 15, 5), (3, 50, 40),(4,20,5)] Name3 22/05/2018 [(4, 2, 1), (5, 2, 2), (6, 50, 3), (1,2,3)] Name4 23/05/2018 [(1, 3, 10), (2, 1, 5), (3, 2, 40), (6,20,20)]

在Haskell中,我创建了这个函数来读取文件的内容并将此内容“转换”为我的自定义类型。

rlist :: String -> [(Int, Int, Int)] rlist = read loadPurchases :: IO [(String, String, [(Int, Int, Int)])] loadPurchases = do s <- readFile "tes.txt" return (glpurch (map words (lines s))) glpurch :: [[String]] -> [(String, String, [(Int, Int, Int)])] glpurch [] = [] gplpurch ([name, dt, c]:r) = (name, dt, (rlist c)) : gplpurch r

但是当我尝试执行“loadPurchases”函数时,我得到了这个错误:函数glpurch中的非详尽模式。

使用:set -Wall,我收到了这条帮助信息:

<interactive>:6:1: warning: [-Wincomplete-patterns] Pattern match(es) are non-exhaustive In an equation for `glpurch': Patterns not matched: ([]:_:_) ([_]:_) ([_, _]:_) ((_:_:_:_:_):_)

我的问题是如何创建所有这些条件。

如果有人能帮助我创造那些可能决定“停止条件”的条件,我将非常感激

Before to mark this question as duplicated, I already read this topic: Haskell read Integer and list of lists from file and the solution doesn't solve my problem.

I'm trying to read the content in a File that contains this structure:

String, String, [(Int, Int, Int)]

The file looks something like this:

Name1 22/05/2018 [(1, 5, 10), (2, 5, 5), (3, 10, 40)] Name2 23/05/2018 [(1, 10, 10), (2, 15, 5), (3, 50, 40),(4,20,5)] Name3 22/05/2018 [(4, 2, 1), (5, 2, 2), (6, 50, 3), (1,2,3)] Name4 23/05/2018 [(1, 3, 10), (2, 1, 5), (3, 2, 40), (6,20,20)]

In Haskell, I created this function to read the contents of the file and "convert" this content to my custom type.

rlist :: String -> [(Int, Int, Int)] rlist = read loadPurchases :: IO [(String, String, [(Int, Int, Int)])] loadPurchases = do s <- readFile "tes.txt" return (glpurch (map words (lines s))) glpurch :: [[String]] -> [(String, String, [(Int, Int, Int)])] glpurch [] = [] gplpurch ([name, dt, c]:r) = (name, dt, (rlist c)) : gplpurch r

But when I try to execute the "loadPurchases" function, I get this error: Non-exhaustive patterns in function glpurch.

Using :set -Wall, I received this help message:

<interactive>:6:1: warning: [-Wincomplete-patterns] Pattern match(es) are non-exhaustive In an equation for `glpurch': Patterns not matched: ([]:_:_) ([_]:_) ([_, _]:_) ((_:_:_:_:_):_)

My problem is how to create all these conditions.

I will be very grateful if anyone can help me create those conditions that are likely to determine the "stopping condition"

最满意答案

您只匹配长度为3的列表,实际上每行上有更多的单词。 只需在GHCi中尝试:

> words "Name1 22/05/2018 [(1, 5, 10), (2, 5, 5), (3, 10, 40)]" ["Name1","22/05/2018","[(1,","5,","10),","(2,","5,","5),","(3,","10,","40)]"]

你可能想重新组合前两个词的所有单词:

glpurch ((name : dt : rest) :r) = (name, dt, (rlist $ unwords rest)) : gplpurch r

You are only matching lists of length 3 when in fact there are many more words on each line. Just try it in GHCi:

> words "Name1 22/05/2018 [(1, 5, 10), (2, 5, 5), (3, 10, 40)]" ["Name1","22/05/2018","[(1,","5,","10),","(2,","5,","5),","(3,","10,","40)]"]

You probably want to recombine all words past the first two:

glpurch ((name : dt : rest) :r) = (name, dt, (rlist $ unwords rest)) : gplpurch r