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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:51:29+00:00 2026-06-13T07:51:29+00:00

I have a snippet of code that looks something like this: data SomeData =

  • 0

I have a snippet of code that looks something like this:

data SomeData = A | B | C | D | E deriving (Show, Enum)

and I want to be able to map certain values to these types easily. Say I wanted to have chars mapped to a SomeData type, I might do it like so:

mappings = zip "abcde" [A, B, C, D, E]

This would produce [('a',A),('b',B)...] and so on. I was hoping that the Enum type class might allow me to be able to do this easily by converting the data type into the list I described above ([A, B, C, D, E]) by use of some convenience function.

Is there such a function in the standard library? Or would I need to define it myself if I wanted to use something like this?

  • 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-13T07:51:30+00:00Added an answer on June 13, 2026 at 7:51 am

    Generally, I’d do

    mappings = zip "abcde" [A..]
    

    but I’d be very tempted to go with

    mappings = zip ['a' ..] [A ..]
    

    so that if I added other letters later, the code would update.

    Going with maximally future-proof work I’d define

    import Data.Char
    
    data SomeData = A | B | C | D | E deriving (Show, Enum, Bounded)
    
    allData :: SomeData
    allData = enumFrom minBound
    
    mapppings = zip (map (toLower.head.show) allData) allData
    

    where the only assumtion I’m making about the future is that it consists of single letters.

    However, this is playing and not in fact all that clear.

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

Sidebar

Related Questions

I have a snippet of code that looks like this: double Δt = lastPollTime
I have a code snippet that looks like this: [tableView beginUpdates]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
I have code that looks like the following: //unrelated code snipped resolver.reset(new tcp::resolver(iosvc)); tcp::resolver::query
I have this code snippet inside a function that checks if an object exists
Today I noticed a snippet of code that looks like the one below: public
I have a short snippet of C# code like this: HtmlGenericControl titleH3 = new
So, say I have an array that looks like this: t = [ [
I have a snippet of code im trying to parse with nokogiri that looks
I would like to have a custom snippet of html form code that takes
In my code right now I have something like this: state.BytesRead += readPacket.Result; if

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.