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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:46:34+00:00 2026-05-15T20:46:34+00:00

parserChar :: Char -> Parser Char parserChar c = Parser ch where ch d

  • 0
parserChar :: Char -> Parser Char
parserChar c = Parser ch where
   ch d = case dvChar d of
      Parsed c dp -> Parsed c dp
      _ -> NoParse

The above function is supposed to take a Char c and return a Parser which will only match c. The function dvChar d is going to return either Parsed char dp or NoParse (where char is the next character in the string). Thus, I had hoped that Parsed c dp would only match a result in which char==c, but what’s actually happening is that the Parser returned by this function matches any character (even though c seems to be bound to some particular Char, as a function argument).

The following function works correctly:

parserChar :: Char -> Parser Char
parserChar c = Parser ch where
   ch d = case dvChar d of
      Parsed char dp -> if char == c then Parsed char dp else NoParse
      _ -> NoParse

Manually coding the parser to parse the letter 'a' also works correctly, in that

case dvChar d of
   Parsed 'a' dp -> Parsed 'a' dp
   _ -> NoParse

will only return success if the character was 'a'.

So what gives? Can you only match literals in a pattern like that (e.g. despite the fact that Char is in class Eq, the if char==c (..) still needs to be coded manually), or am I doing something wrong?

  • 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-15T20:46:35+00:00Added an answer on May 15, 2026 at 8:46 pm

    Yes, you can only match literals. In fact, a better way to think about it is that you can only match constructors, and it so happens that Int, Char, String & co. all have literal constructors.

    Note that you can also mix case and guards and write it as (from memory):

    parserChar :: Char -> Parser Char
    parserChar c = Parser ch where
       ch d = case dvChar d of
          Parsed char dp | char == c -> Parsed char dp
          _ -> NoParse
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> #include<stdlib.h> #include<string.h> #include<stdbool.h> char** parser(char *message) { char a[9][256]; char* tmp =message; bool
I am calling a function recursively and which is allocating 2048 byte is being
i am trying to make sense of the following result. The test case code
I have a function that returns the first n characters until a specified character
struct Message { char type; double idNum; char *Time; char *asset; bool BS; float
Can someone help me to complete my code with a function that can check
Consider the following piece of Haskell code: type Parser a = String -> [(a,
I have a template function specified: template<class T> void jsonParse(json_object* jobj, eParseWhat parseWhat, T
Hi i'm facing the following problem, i have to re-write an existant code to
Is there a better way to do this? /* Compares two characters. If ch1

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.