[Haskell-cafe] How to implement Read instance for user defined type

Verma Anurag-VNF673 Anurag.Verma at motorola.com
Thu Mar 20 10:10:11 EDT 2008


Hi,

I am trying to understand how to define instances for Read class for a
user defined type. 

 

This is the sample code I wrote, but I am not able to get it correctly.
So, let me know what's wrong with this program:

 

 

module Mark where

 

data Mark = Mark Int deriving (Show)

 

instance Read Mark where

 

            readsPrec _ str = [(Mark x, t') | ("mark",t) <- reads str, 

                                                             (x,t') <-
reads t

 

When I run it in ghci:

 

*Mark> reads "mark 5" :: [ (Mark,String)]         ( I am intentionally
using mark in lowercase) 

 []

 

Why is the output coming out as [] as against expected output [(Mark 5,
"")] ?

 

-Anurag

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080320/6938f05f/attachment.htm


More information about the Haskell-Cafe mailing list