读取Haskell中的值和列表列表

时间:2018-06-07 18:28:48

标签: file haskell

在将此问题标记为重复之前,我已经阅读了此主题:Haskell read Integer and list of lists from file并且该解决方案无法解决我的问题。

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

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:
        ([]:_:_)
        ([_]:_)
        ([_, _]:_)
        ((_:_:_:_:_):_)

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

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

2 个答案:

答案 0 :(得分:7)

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

MATCH (s:ServiceRequest {srStatus: "Open"} OR {srStatus: "Pending"}) -[:FOR]->(p:Product) RETURN p

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

> 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)]"]

答案 1 :(得分:0)

为了解决我的问题,我做了@Welperooni和@Thomas M. DuBuisson建议的事情。

我将此代码添加到我的函数中:

glpurch ((name: dt: c: _): r) = (name, dt, (read c :: [(Cod, Quant, Price)

我删除了文件列表中的空白,这些空格使得文本的划分没有正确完成。