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

Verma Anurag-VNF673 Anurag.Verma at motorola.com
Mon Mar 24 07:40:18 EDT 2008


 
That works, but is probably not what you want. You can use the lex 
function to parse identifiers not enclosed in quotes:
 
 > instance Read Mark where
 >   readsPrec _ str = [(Mark x, t') | ("mark",t) <- lex str,
 >                                     (x,t') <- reads t
 

I played a bit around with lex function and it seems that under certain
circumstances it doesn't read the string of token properly 

 

For e.g. 

 

 

-- IP address 

 

*Mark> lex "192.168.0.1"

[("192.168",".0.1")]

 

-- digit + char 

 

Also lex "8021p"   output is 

 

*Mark> lex "8021p"

[("8021","p")]

 

Is there any other lex variant available?  Or should I switch to using
Parsec library, in that case, will I still be able to use it with Read
instances? If so, how?

 

Anurag

 

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


More information about the Haskell-Cafe mailing list