我正在尝试从文件中读取两个自己的数据类型“BoardEdge”的列表。当我尝试运行代码时,出现异常:
“Main.hs:Prelude.read:没有解析”
正如我怀疑我在负责验证输入(validateInput)的函数上得到这个。当我在 ghci 中尝试插入两个 BoardEdge“对象”时,它运行良好并给出了 True。
谁能给我建议我做错了什么以及如何解决问题?
数据类型:
data Field = Empty | Black | Yellow deriving (Eq, Ord, Enum, Show, Read)
data BoardEdge = BoardEdge { colRow :: [[(Field, Int)]]} deriving (Read, Eq, Ord, Show)
主文件
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)]]