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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:55:25+00:00 2026-06-14T21:55:25+00:00

I am trying to write instance of Read class to read an input as

  • 0

I am trying to write instance of Read class to read an input as string as follow:

"1 3 -    
 - 2 3      
 - - 5" 

Convert it into [[Maybe Int]]

“-” will be translated into Nothing
“3” will be Just 3

"('a',3) - ('b',7)
 ('c',5) ('e',0) -  
 ('d',9) - ('h',8)"

Convert it into [[Maybe (Char,Int)]]

“-” will be translated into Nothing
“(‘a’,3)” will be Just ('a',3)

I tried to write them by handling with List of Char, but it takes a lot of work. Do you have any suggestion? Sorry, I am quite new to Haskell, so I ask you that question. 🙁

  • 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-14T21:55:26+00:00Added an answer on June 14, 2026 at 9:55 pm

    If you got rid of the - entries, you could do this very quickly as

    Prelude> (map (map read) . map words. lines $ "('a',3) ('b',4)\n('c',5)")::[[(Char,Int)]]
    [[('a',3),('b',4)],[('c',5)]]
    

    Or define it as a function

    genericReadLines :: Read a => String -> [[a]]
    genericReadLines = map (map read) . map words. lines
    

    Which you can use thus:

    *Main> (genericReadLines "('a',3) ('b',4)\n('c',5)")::[[(Char,Int)]]
    [[('a',3),('b',4)],[('c',5)]]
    

    but you may find it easier to do

    readCharInts :: String -> [[(Char,Int)]]
    readCharInts = genericReadLines
    
    readInts :: String -> [[Int]]
    readInts = genericReadLines
    

    So you can just type

    *Main> readInts "1 2 3\n4 5 6\n7 8 9"
    [[1,2,3],[4,5,6],[7,8,9]]
    *Main> readCharInts "('a',3) ('b',4)\n('c',5)"
    [[('a',3),('b',4)],[('c',5)]]
    

    But what about keeping the -? You’ll have to use a Maybe data type, to represent not having a value for certain points in your list; we can use - as shorthand for Nothing and a as shorthand for Just a.

    read' :: Read a => String -> Maybe a
    read' "-" = Nothing
    read' xs = Just (read xs)
    

    I should warn you that that code is fragile if your data could possibly be '-', but perhaps it can’t.

    genericMaybeReadLines :: Read a => String -> [[Maybe a]]
    genericMaybeReadLines = map (map read') . map words. lines
    

    Then we can have

    readMaybeCharInts :: String -> [[Maybe (Char,Int)]]
    readMaybeCharInts = genericMaybeReadLines
    
    readMaybeInts :: String -> [[Maybe Int]]
    readMaybeInts = genericMaybeReadLines
    

    So now we can do

    *Main> readMaybeCharInts "('a',3) ('b',4)\n- ('c',5)"
    [[Just ('a',3),Just ('b',4)],[Nothing,Just ('c',5)]]
    *Main> readMaybeInts "2 3 -\n4 - 2"
    [[Just 2,Just 3,Nothing],[Just 4,Nothing,Just 2]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a generic class to write and read Objects to/from
I am trying to write a script that will read a remote sitemap.xml and
Trying to write app for service technicians that will display open service calls within
Trying to write a couple of functions that will encrypt or decrypt a file
Trying to write a javascript bookmarklet to jump from a front end link into
I'm trying to write a class that has a generic member variable but is
I am trying to write a WCF Servce which will reside in a Windows
I'm trying to write a class that would enable me to just write .save();
I'm trying to write a method that will return a Hibernate object based on
I am trying write a script where when a user clicks on a link,

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.