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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:40:30+00:00 2026-06-15T05:40:30+00:00

I want to implement an instance of read that enables me to read a

  • 0

I want to implement an instance of read that enables me to read a string (ex: “- – 8 – 3 -“) and construct a list containing those values.

data Value a = Nul | Val a

showsValue :: (Show a) => Value a -> ShowS
showsValue (Val x) =  ("Value" ++) . shows x
showsValue (Nul)   =  ("Nothing 0" ++)

instance Show a => Show (Value a) where
    showsPrec _ x = showsValue x

instance Read a => Read (Value a) where
    readsPrec _ m = readsMatrix m

readsMatrix :: (Read a) => ReadS (Value a)
readsMatrix ('-':s) = [(Nul, rest) | (' ',rest) <- reads s]
readsMatrix s       = [(Val x,rest)| (x,' ':rest) <- reads s]

After performing this test:

read "- 8 - - 3" :: Value Int

I get the error *** Exception: Prelude.read: no parse

What am I doing wrong here?

update

readsMatrix ('-':s) = [(Nul, dropWhile isSpace s)]
readsMatrix s = [(Val x, dropWhile isSpace rest) | (x,rest) <- reads s]
  • 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-15T05:40:33+00:00Added an answer on June 15, 2026 at 5:40 am

    Correcting read for Value a

    First, don’t bother removing spaces, that’s all handled fine by reads:

    readsMatrix :: (Read a) => ReadS (Value a)
    readsMatrix ('-':s) = [(Nul, dropWhile (==' ') s)]
    readsMatrix s       = [(Val x,rest)| (x,rest) <- reads s] 
    

    Your read instance is now fine for single Values:

    *Main> read "4" :: Value Int
    Value4
    *Main> read "-" :: Value Int
    Nothing 0
    

    Correcting read for [Value a]

    But you want to read lists as separated by spaces, so since that’s non-standard behaviour, you’ll need to write a custom readList :: :: ReadS [Value a]

    instance Read a => Read (Value a) where
        readsPrec _ m = readsMatrix m
        readList s = [(map read.words $ s,"")]
    

    So now

    *Main> read "- 4 2 - 5" :: [Value Int]
    [Nothing 0,Value4,Value2,Nothing 0,Value5]
    

    but unfortunately,

    *Main> read "- 4 2 \n- 5 4" :: [Value Int]
    [Nothing 0,Value4,Value2,Nothing 0,Value5,Value4]
    

    and even worse,

    *Main> read "- 4 2 \n- 5 4" :: [[Value Int]]
    *** Exception: Prelude.read: no parse
    

    Reading a matrix

    There’s no straightforward way round this that I can see, because there’s no readListOfLists in the Read class, so why not make a standalone function

    matrix :: Read a => String -> [[Value a]]
    matrix = map read.lines
    

    so that

    *Main> matrix "3 4 -\n3 - 6\n4 5 -" :: [[Value Int]]
    [[Value3,Value4,Nothing 0],[Value3,Nothing 0,Value6],[Value4,Value5,Nothing 0]]
    

    Show isn’t what I would choose

    I think your Show instance for Value is a little misleading (Nul doesn’t have a zero on it, Val isn’t written Value). Either write

    data Value a = Nul | Val a  deriving Show
    

    so that it looks as it is or define it to match the Read instance

    instance Show a => Show (Value a) where
      show Nul = "-"
      show (Val a) = show a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I want to implement a list of ViewPagers. Every item of a ListView is
I want implement in my software solution an VBA editor but in c# 3.0.
i starter in jqgrid, i want implement inline edit in jqgrid i have this
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
I want to implement a simple script to do some boring housekeeping on my
I want to implement charting in SWT and I want to use GC.transform to
I want to implement an application which will work as a parser. User will
I want to implement a job scheduler in my windows azure application. My aim
I want to store a bunch of key value pairs, with the key being

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.