Haskell:解析数据类型错误

时间:2016-07-07 18:25:28

标签: haskell

我有一个字符串,我需要解析为Array of Array或其他。

这是我的代码

import Test.HUnit (runTestTT,Test(TestLabel,TestList),(~?=))
import qualified Text.Parsec as P (char,runP,noneOf,many,(<|>),eof)
import Text.ParserCombinators.Parsec
import Text.Parsec.String 
import Text.Parsec.Char
import Text.PrettyPrint.HughesPJ
import Data.Maybe
import Data.List.Split

newtype CSV = CSV [Row] deriving (Show,Eq)
type Row = [String]

parseCSV :: Parser CSV
parseCSV = error "not done yet"  

runParsec :: Parser a -> String -> Maybe a
runParsec parser input = case P.runP parser () "" input of
    Left  _ -> Nothing
    Right a -> Just a

当我开始实现parseCSV函数

parseCSV = Just (CSV [[""]])

我收到此错误:

Expected type: Parser CSV
Actual type: Maybe CSV

1 个答案:

答案 0 :(得分:1)

请改为尝试:

parseCSV :: Parser CSV
parseCSV = return $ CSV [[""]]

错误说明了一切:parseCSV应该有Parser CSV类型(来自类型签名),但Just (CSV [[""]])的类型为Maybe CSV