"Prelude.read: no parse", 自己的数据类型

"Prelude.read: no parse", own data type

我正在尝试从文件中读取两个自己数据类型的列表 'BoardEdge'。当我尝试 运行 代码时出现异常:

"Main.hs: Prelude.read: no parse"

正如我怀疑的那样,我在负责输入验证 (validateInput) 的函数上得到了这个。当我在 ghci 中插入两个 BoardEdge 'objects' 尝试此功能时,它运行良好并给出 True。

任何人都可以向我提供建议,我做错了什么以及如何解决问题?

数据类型:

data Field = Empty | Black | Yellow deriving (Eq, Ord, Enum, Show, Read)

data BoardEdge = BoardEdge { colRow :: [[(Field, Int)]]} deriving (Read, Eq, Ord, Show) 

Main.hs

    main :: IO()
    main = do
      args <- getArgs
      input <- loadInput args
      putStrLn "Puzzle input loaded:"
      putStrLn input
      let parsedInput = parseInput input
      if (validateInput parsedInput)
        then putStrLn "Input is valid."
        else error "Input invalid!"

    -- asks for path and reads input file
    loadInput :: [String] -> IO String
    loadInput [] =  getPath >>= readFile where
      getPath = do
        putStrLn "Provide path to puzzle input file:"
        getLine
    loadDefinition (a:_) = readFile a


    -- get valid data from input file
    parseInput :: String -> (B.BoardEdge,B.BoardEdge)
    parseInput d = parseInput' $ lines d where
      parseInput' (columns: rows :_) =
        (read columns, read rows)

Board.hs 中的验证函数导入合格为 B:

    validateInput :: (B.BoardEdge,B.BoardEdge) -> Bool
    validateInput (columns, rows) = rowColEq where
      rowColEq = countBlocks columns == countBlocks rows


    -- function that counts total quantity of colored blocks
    countBlocks :: (B.BoardEdge)-> Int
    countBlocks (B.BoardEdge colRow) = countBlocks' $ concat colRow where
      countBlocks' [] = 0
      countBlocks' (x:xs) = snd x + countBlocks' xs

我的输入文件是这样的:

    [[(Black,2),(Yellow,2),(Black,1)],[(Black,2),(Yellow,1),(Black,3)]]
    [[(Black,5)],[(Black,2),(Black,1)],[(Black,2),(Black,2)],[(Black,1),(Black,2)]]

将您自己的代码片段和 Fyodor 的评论结合在一起:

Prelude> data Field = Empty | Black | Yellow deriving (Eq, Ord, Enum, Show, Read)
Prelude> data BoardEdge = BoardEdge { colRow :: [[(Field, Int)]]} deriving (Read, Eq, Ord, Show)
Prelude> let edge1 = BoardEdge [[(Black,2),(Yellow,2),(Black,1)],[(Black,2),(Yellow,1),(Black,3)]]
Prelude> show edge1
"BoardEdge {colRow = [[(Black,2),(Yellow,2),(Black,1)],[(Black,2),(Yellow,1),(Black,3)]]}"
Prelude> let correctInput = it -- ^^ the above

所以现在我们知道 read 期望什么,这就是 show 产生的。这与您用作输入的内容相比如何?

Prelude> let myInput =     "[[(Black,2),(Yellow,2),(Black,1)],[(Black,2),(Yellow,1),(Black,3)]]"
Prelude> correctInput == myInput
False

或不那么尖刻:如果您希望默认 read 实例解析您的输入,那么您的输入必须是正确的 Haskell 代码以及数据构造函数和所有内容。在这种情况下,需要使用 BoardEdge