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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:34:51+00:00 2026-06-18T18:34:51+00:00

i have a data structure like this data Something = Something Integer String String

  • 0

i have a data structure like this

data Something = Something Integer String String

and i want to convert

["Something", "3", "text", "42"] 

to the data.

for now, i have

altRead :: Read a => [String] -> a
altRead = read . unwords . hack
    where 
        hack = map (\x -> if isNumber x then x else "\"" ++ x ++ "\"")
        isNumber = foldl (\b c -> isDigit c && b) True 

but i forgot, that some numbers could be strings in the data structure.
is there a simple solution for this or do i need to write a alternative read typeclass?

  • 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-18T18:34:52+00:00Added an answer on June 18, 2026 at 6:34 pm

    You’re writing a tiny parser atop some lexed tokens. You can’t really implement a Read instance since read :: Read a => String -> a and you want to do [String] -> a for a == Something. You can take advantage of Read instances that already exist, though, to bootstrap parsing your Integer, for instance.

    So let’s try it. We’ll parse a Something from the list of tokens.

    import Safe -- gives us readMay :: Read a => String -> Maybe a
    
    parseSomething :: [String] -> Maybe Something
    parseSomething ("Something":strInt:stra:strb:_) = 
      do int <- readMay strInt
         return $ Something int stra strb
    parseSomething _ = Nothing
    

    We could do it a little more compactly using Maybe as an Applicative, too

    import Control.Applicative
    
    parseSomething :: [String] -> Maybe Something    
    parseSomething ("Something":strInt:stra:strb:_) = 
      Something <$> readMay strInt <*> pure stra <*> pure strb
    parseSomething _ = Nothing
    

    Really, we should probably return any unconsumed tokens as well so we can continue parsing.

    parseSomething :: [String] -> (Maybe Something, [String])
    parseSomething ("Something":strInt:stra:strb:rest) = 
      (Something <$> readMay strInt <*> pure stra <*> pure strb, rest)
    parseSomething rest = (Nothing, rest)
    

    The reason I bring in all this structure to your parse is that this starts to head toward the space of parser combinators like Parsec. Whenever you’ve got a need for a complicated Read it begins to become useful to look at some of the really nice parsing libraries in Haskell.

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

Sidebar

Related Questions

i have a data structure like this public class Employee { public string Name
In C++ I have data structure something like this: struct Data { int N;
I have a similar data structure like this: struct Data { std::string id; Blob
I have a data structure something like this: typedef struct tagSUB_DATA { double measuredValue;
I have a data-structure which is something like this: The population of three cities
My table have data structure like this cate_id task_id date_start date_end other 34 14
Using EF4 and Linq I have a data structure which looks like this: parentid
I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I have a JSON-like data structure (which I don't want to change) that is
I have something like the following data structure: Category StartDateTime EndDateTime =============================================== 1 12/1/2009

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.