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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:15:19+00:00 2026-06-18T05:15:19+00:00

I have worked my way through the Haskell Koans provided here: https://github.com/roman/HaskellKoans I am

  • 0

I have worked my way through the Haskell Koans provided here:
https://github.com/roman/HaskellKoans

I am stuck on the last two Koans, both involving parsing custom algebraic data types. Here is the first:

data Atom = AInt Int | ASym Text deriving (Eq, Show)

testAtomParser :: Test
testAtomParser = testCase "atom parser" $ do
    -- Change parser with the correct parser to use
    --
    let parser = <PARSER HERE> :: P.Parser Atom
    assertParse (ASym "ab") $ P.parseOnly parser "ab"
    assertParse (ASym "a/b") $ P.parseOnly parser "a/b"
    assertParse (ASym "a/b") $ P.parseOnly parser "a/b c"
    assertParse (AInt 54321) $ P.parseOnly parser "54321"

How can define the variable parser such that it can parse the algebraic datatype Atom to pass the assertions?

  • 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-18T05:15:21+00:00Added an answer on June 18, 2026 at 5:15 am

    I.

    Parsers of an ADT tend to reflect the shape of the ADT. Your ADT is formed of two disjoint parts, so your parser probably has two disjoint parts as well

    atom = _ <|> _
    

    II.

    Assuming we know how to parse a single digit (let’s call that basic parser digit) then we parse a (non-negative) integer by just repeating it.

    natural = let loop = digit >> loop in loop
    

    this successfully parses an infinite stream of digits and throws them away. Can we do better? Not with just a monad instance, unfortunately, we need another basic combinator, many, which modifies some other parser to consume input 0 or more times, accumulating the results into a list. We’ll actually adjust this slightly since an empty parse isn’t a valid number

    many1 p = do x  <- p
                 xs <- many p
                 return (x:xs)
    
    natural' = many1 digit
    

    III.

    What about atoms? To pass the test cases, it appears that an atom must be 1-to-many alphanumeric characters or backslashes. Again, this disjoint structure can be immediately expressed in our parser

    sym = many1 (_ <|> _)
    

    We’ll again use some built-in simple parser combinators to build up what we want, say satisfy :: (Char -> Bool) -> Parser Char which matches any character which satisfies some predicate. We can immediately build another useful combinator, char c = satisfy (==c) :: Char -> Parser Char and then we’re done.

    sym = many1 (char '/' <|> satisfy isAlpha)
    

    where isAlpha is a predicate much like the regex [a-zA-Z].

    IV.

    So now we have the core of our parser

    natural <|> sym :: Parser String
    

    the many1 combinators lift our character parsers into parsers of lists of characters (Strings!). This lifting action is the basic idea for building ADT parsers, too. We want to lift our Parser String up into Parser Atom. One way to do it would be to use a function toAtom :: String -> Atom which we could then fmap into the Parser

    atom' :: Parser Atom
    atom' = fmap toAtom (natural <|> sym)
    

    but a function with type String -> Atom defeats the purpose of building a parser in the first place.

    As stated in I. the important part is that the shape of the ADT is reflected in the shape of our atom parser. We’ll need to take advantage of that to build our final parser.

    V.

    We need to take advantage of information in the structure of our atom parser. Let’s instead build two functions

    liftInt :: String -> Atom  -- creates `AInt`s
    liftSym :: String -> Atom  -- creates `ASym`s
    
    liftInt = AInt . read
    liftSym = ASym
    

    each of which stating both a method of turning Strings into Atoms but also declaring what kind of Atom we’re dealing with. It’s worth noting that liftInt will throw a runtime error if we pass it a string that cannot be parsed into an Int. Fortunately, that’s exactly what we know we have.

    atomInt :: Parser Atom
    atomInt = liftInt <$> natural
    
    atomSym :: Parser Sym
    atomSym = liftSym <$> sym
    
    atom'' = atomInt <|> atomSym
    

    Now our atom'' parser takes advantage of the guarantee that natural will only return strings which are valid parses for a natural—our call to read will not fail!—and we try to build both AInt and ASym in order, trying one after another in a disjoint structure just like the structure of our ADT.

    VI.

    The whole shebang is thus

    atom''' =     AInt . read <$> many1 digit
              <|> ASym <$> many1 (    char '/' 
                                  <|> satisfy isAlpha)
    

    which shows the fun of parser combinators. The whole thing is built up from the ground using tiny, composable, simple pieces. Each one does a very tiny job but all together they span a large space of parsers.

    You can also easily augment this grammar with more branches in your ADT, a more thoroughly specified symbol type parser, or failure decorations with <?> so that you have great error messages on failed parses.

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

Sidebar

Related Questions

I have worked through the answer provided here . I have been able to
I've worked my way through a number of interesting routing problems - turning a
I have worked on generating PDF about 3 years ago and used FPDF lib
i have worked for a year now , on server side applications and everytime
I have worked with Apache before, so I am aware that the default public
I have worked on this problem for my entire day and can't solve it.
I have worked within a web development company where we had our local machines,
I have worked with SOAP in SAAJ and JAXM, and I want to extend
I have worked with pointers a lot, but still whenever I work with them,
I have worked many projects with no problems. But in current project, I tested

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.