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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:41:48+00:00 2026-05-27T20:41:48+00:00

I am translating some code from attoparsec to Parsec, because the parser needs to

  • 0

I am translating some code from attoparsec to Parsec, because the parser needs to produce better error messages. The attoparsec code uses inClass (and notInClass) extensively. Is there a similar function for Parsec that lets me translate inClass-occurences mechanically? Hayoo and Hoogle didn’t offer any insight into the matter.

inClass :: String -> Char -> Bool

inClass "a-c'-)0-3-" is equivalent to \ x -> elem x "abc'()0123-", but the latter is inefficient and tedious to write for large ranges.

I will reimplement the function myself if nothing else is available.

  • 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-27T20:41:49+00:00Added an answer on May 27, 2026 at 8:41 pm

    There isn’t any such combinator; if there was, it would be in Text.Parsec.Char (which is where all the standard parser combinator functions that involve Char are defined). You should be able to define it fairly easily.

    I don’t think you’ll be able to get the same performance advantages attoparsec does with its implementation, though; it relies on the internal FastSet type, which only works with 8-bit characters. Of course, if you don’t need Unicode support, that might not be a problem, but the code for FastSet implies you’ll get unpredictable results passing Chars greater than '\255', so if you want to reuse the FastSet-based solution, you’ll at least have to read the strings you’re parsing in binary mode. (You’ll also have to copy the implementation of FastSet into your program, as it’s not exported…)

    If your range strings are short, then a simple solution like this is likely to be pretty fast:

    type Range = (Char, Char)
    
    inClass :: String -> Char -> Bool
    inClass = inClass' . parseClass
    
    parseClass :: String -> [Range]
    parseClass "" = []
    parseClass (a:'-':b:xs) = (a, b) : parseClass xs
    parseClass (x:xs) = (x, x) : parseClass xs
    
    inClass' :: [Range] -> Char -> Bool
    inClass' cls c = any (\(a,b) -> c >= a && c <= b) cls
    

    You could even try something like this, which should be at least as efficient as the above version (including when many calls to a single inClass s are made), and additionally avoid the list traversal overhead:

    inClass :: String -> Char -> Bool
    inClass "" = const False
    inClass (a:'-':b:xs) = \c -> (c >= a && c <= b) || f c where f = inClass xs
    inClass (x:xs) = \c -> c == x || f c where f = inClass xs
    

    (taking care to move the recursion out of the lambda; I don’t know if GHC can/will do this itself.)

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

Sidebar

Related Questions

I'm translating some code from the DataContractSerializer to the impressive protobuf-net serializer, and one
I am translating some code from VB to C# and came across this: Format(seg.R,
I need help with translating some code from VB to C#. Public Function ToBase36(ByVal
I'm translating some code from MATLAB to Python and I'm stuck with the corrmtx()
I am translating some code from lisp to Python. In lisp, you can have
I'm teaching myself Python and I was translating some sample code into this class
I'm actually translating some C++ code (which I know very little about, and have
I'm having problems translating this code to C# from VB.NET. This code is supposed
I'm testing some code in C# from Visual Studio Express 2008: delegate void Hm(int
I'm borrowing some code from the VLC to my video player, written in MSVC++

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.