John Doe
John Doe

Reputation: 25

Haskell: Data Type error by parsing

I have a string that I Need to parse to Array of Array or otherwise.

This is the Code I have

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

When I start implementing parseCSV function

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

I get this error:

Expected type: Parser CSV
Actual type: Maybe CSV

Upvotes: 0

Views: 74

Answers (1)

Alec
Alec

Reputation: 32329

Try this instead:

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

The error says it all: parseCSV should have type Parser CSV (from the type signature), but Just (CSV [[""]]) has type Maybe CSV.

Upvotes: 1

Related Questions