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

  • Home
  • SEARCH
  • 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 9102979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:31:06+00:00 2026-06-17T01:31:06+00:00

I’m a beginner with Haskell, so it might be very obvious what I’m doing

  • 0

I’m a beginner with Haskell, so it might be very obvious what I’m doing wrong…

While trying to parse "1:1,2, 2:18, 3:100" into [(1,1), (1,2), (2,18), (3,100)] I got stuck on a lookahead.

To know if a number is a verse number it should look ahead for a colon, because then it is a chapter number instead.

The problem lies in the last function verseNr, it should parse+consume the number if not followed by a colon, otherwise fail without consuming anything (leaving the number to be parsed as a chapter number by refGroupByChapter).

Except for this issue it seems to work nicely 🙂

import Text.ParserCombinators.Parsec

main = do
  case (parse refString "(unknown)" "1:1,2, 2:18, 3:100") of
    Left  e -> do putStr "parse error at "; print e
    Right x -> print x  -- expecting: [(1,1), (1,2), (2,18), (3,100)]

refString :: GenParser Char st [(Int, Int)]
refString = do
  refGroups <- many refGroupByChapter
  eof
  return $ concat $ map flatten refGroups
  where flatten (_, [])   = []
        flatten (c, v:vs) = (c, v):(flatten (c, vs))

refGroupByChapter :: GenParser Char st (Int, [Int])
refGroupByChapter = do
  chapterNum <- many digit
  char ':'
  verseNums <- verseNrs
  return ((read chapterNum :: Int), verseNums)

verseNrs :: GenParser Char st [Int]
verseNrs = do
  first <- verseNr
  remaining <- remainingVerseNrs
  return (first:remaining)
  where
    remainingVerseNrs = do  -- allow for spaces around the commas
      (spaces >> oneOf "," >> spaces >> verseNrs) <|> (return [])
    verseNr = try $ do
      n <- many1 digit
      notFollowedBy $ char ':'  -- if followed by a ':' it's a chapter number
      return (read n :: Int)
  • 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-17T01:31:07+00:00Added an answer on June 17, 2026 at 1:31 am

    The trick for your particular problem would be to use the sepBy family of functions. You’re parsing lists of numbers separated by commas, which is exactly what sepBy is for. A list of verses has the following properties: there has to be at least one verse number and there is a trailing comma. Combining the two, we realize we need the sepEndBy1 function. These functions are usually written in an infix position, so your code would look something like this:

    verseNrs = verseNr `sepEndBy1` (spaces >> char ',' >> spaces)
    

    I don’t think you need to change anything else to get the code to work.

    A couple of other minor style notes: you have some unnecessary parentheses. This isn’t important, it just annoys me personally. E.g. in case ... of you do not need parens around the ... bit. Also, you do not need the type signature when you use read–the compiler can infer the type. That is, since verseNrs returns [Int], it’s completely clear both to the compiler and to me that read n produces an Int. There is no need to say it explicitly.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I am using JSon response to parse title,date content and thumbnail images and place
I've got a string that has curly quotes in it. I'd like to replace
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only

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.