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 8510197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:38:15+00:00 2026-06-11T03:38:15+00:00

I’m learning haskell, and my current project is writing a parser to read a

  • 0

I’m learning haskell, and my current project is writing a parser to read a text file representation of a database.

At the moment, I’m setting up the code for reading individual fields of tables. In the text file, fields look either like this:

name type flags format

or this:

name type       format

This gives the trouble of having to account for cases of there being a flag or not being a flag. I solved this in my main function like this:

main = case parse fieldsWithFlags "(test)" testLine of
        Left err  -> noFlags
        Right res -> print res
   where noFlags = case parse fieldsWithoutFlags "(test)" testLine of
                        Left err -> print err
                        Right res -> print res

If I understand correctly, this says “If it’s a line that doesn’t have flags, try to parse it as such; otherwise, return an error.” It prints the correct results for any “testLine” I throw at it, and returns errors if both options fail. However, when I try to pull this out into its own function, like this:

field :: Either ParseError Field
field = case parse fieldsWithFlags "(test)" testLine of
            Left err  -> noFlags
            Right res -> return Right res
        where noFlags = case parse fieldsWithoutFlags "(test)" testLine of
                           Left err -> return Left err
                           Right res -> return Right res
main = case field of
       Left err -> print err
       Right res -> print res

GHC gives me:

haskellParsing.hs:58:26:
Couldn't match expected type `Either ParseError Field'
with actual type `b0 -> Either b0 b0'
In the expression: noFlags
In a case alternative: Left err -> noFlags
In the expression:
  case parse fieldsWithFlags "(test)" testLine of {
    Left err -> noFlags
    Right res -> return Right res }

I’ve played around with this a lot, but just can’t get it working. I’m sure there’s a much more clear-headed way of doing this, so any suggestions would be welcome – but I also want to understand why this isn’t working.

Full code is at: http://pastebin.com/ajG6BkPU

Thanks!

  • 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-06-11T03:38:16+00:00Added an answer on June 11, 2026 at 3:38 am

    You don’t need the return in your cases. Once you wrap something in Left or Right it is in Either; since you only need a Either ParseError Field, the Left and Right do not need an extra return.

    Also, you should be able to simplify your parseFields significantly. You can write a new parser that looks like this:

    fields = try fieldsWithFlags <|> fieldsWithoutFlags
    

    what this does is run the first one and, if it fails, backtrack and run the second one. The try is important because this is what enables the backtracking behavior. You have to backtrack because fieldsWithFlags consumes some of the input that you care about.

    Now you should be able to just use fields in your main function.

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

Sidebar

Related Questions

I have a reasonable size flat file database of text documents mostly saved in
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

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.