Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3395274
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:16:13+00:00 2026-05-18T04:16:13+00:00

I am new to both Haskell and Parsec. In an effort to learn more

  • 0

I am new to both Haskell and Parsec. In an effort to learn more about the language and that library in particular I am trying to create a parser that can parse Lua saved variable files. In these files variables can take the following forms:

varname = value

varname = {value, value,…}

varname = {{value, value},{value,value,…}}

I’ve created parsers for each of these types but when I string them together with the choice <|> operator I get a type error.

Couldn't match expected type `[Char]' against inferred type `Char'
  Expected type: GenParser Char st [[[Char]]]
  Inferred type: GenParser Char st [[Char]]
In the first argument of `try', namely `lList'
In the first argument of `(<|>)', namely `try lList'

My assumption is (although I can’t find it in the documentation) that each parser passed to the choice operator must return the same type.
Here’s the code in question:

data Variable = LuaString ([Char], [Char])
          | LuaList ([Char], [[Char]])
          | NestedLuaList ([Char], [[[Char]]])
          deriving (Show)

main:: IO()
main = do
       case (parse varName "" "variable = {{1234,\"Josh\"},{123,222}}") of
            Left err -> print err
            Right xs -> print xs 

varName :: GenParser Char st Variable
varName = do{
        vName <- (many letter);
        eq <- string " = ";
        vCon <- try nestList
             <|> try lList 
             <|> varContent;
        return (vName, vCon)}

varContent :: GenParser Char st [Char]
varContent =  quotedString 
    <|> many1 letter
    <|> many1 digit

quotedString :: GenParser Char st [Char]
quotedString = do{
         s1 <- string "\""; 
         s2 <- varContent;
         s3 <- string "\"";
         return (s1++s2++s3)}

lList :: GenParser Char st [[Char]]
lList = between (string "{") (string "}") (sepBy varContent (string ","))

nestList :: GenParser Char st [[[Char]]]
nestList = between (string "{") (string "}") (sepBy lList (string ","))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:16:14+00:00Added an answer on May 18, 2026 at 4:16 am

    That’s correct.

    (<|>) :: (Alternative f) => f a -> f a -> f a
    

    Notice how both arguments are exactly the same type.

    I don’t exactly understand your Variable data type. This is the way I would do it:

    data LuaValue = LuaString String | LuaList [LuaValue]
    data Binding = Binding String LuaValue
    

    This allows values to be arbitrarily nested, not just nested two levels deep like yours has. Then write:

    luaValue :: GenParser Char st LuaValue
    luaValue = (LuaString <$> identifier)
           <|> (LuaList <$> between (string "{") (string "}") (sepBy (string ",") luaValue))
    

    This is the parser for luaValue. Then you just need to write:

    binding :: GenParser Char st Binding
    content :: GenParser Char st [Binding]
    

    And you’ll have it. Using a data type that accurately represents what is possible is important.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to both Haskell and programming. My question about binding in pattern-matched,
When starting a new Haskell project which needs language features that are offered by
I'm kinda new both to OCR recognition and Python. What I'm trying to achieve
I'm new to both Ruby and programming in general. I've been trying to put
So, I'm kinda new to Haskell (and programming generally) and I've been trying to
Being new to both .NET and Silverlight, I have noticed that many of the
I'm pretty new to both PDO and OOP. I'm trying to write a class
New to both Android development and Java, trying to separate my queries into query
I'm kinda new at both programming, and WPF. I know that this is pretty
I'm new both to QT development, and std libraries. At this moment I'm trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.