<div dir="ltr"><div><div><div><div><div><div>The most common way is to just auto derive Read. I'm not sure that that ever really fails. Are you sure the problem isn't with the Show instance of the type? People commonly write invalid Show instances to make them look pretty and they shouldn't. read and show are supposed to be inverses of each other because when they aren't, problems like this occur.<br><br></div>The simple way to do a Read instance is to implement the reads function for it. The confusion comes from its type. readsPrec :: Int -> ReadS a. ReadS is defined as String -> [(a, String)], where a is the parsed result and String is the rest of the string that is being parsed, , which may look confusing, and the Int is precedence, which can usually be ignored. It could have been Int -> String -> Maybe (a, String), but Read predates Maybe. So instead it returns a list and if it fails to parse, it returns [] instead of Nothing. So.<br><br></div>data MyFoo = MyFoo<br><br>instance Read MyFoo where<br> -- readsPrec :: Int -> String -> [(MyFoo, String)]<br> readsPrec _ = readFoo<br><br>readFoo :: String -> [(MyFoo, String)]<br>readFoo str = case splitAt 5 str of<br> ("MyFoo", rest) -> [(MyFoo, rest)]<br> otherwise -> []<br><br><div><div><div>If you need something more complex, there are functions to do it in base that perform lexing and parsing. I have never used them but you can go ahead and read some of the instances such as Ordering at <a href="https://hackage.haskell.org/package/base-4.10.0.0/docs/src/GHC.Read.html#line-398">https://hackage.haskell.org/package/base-4.10.0.0/docs/src/GHC.Read.html#line-398</a> to try and learn how it might work for you.<br><br></div><div>But honestly I think you should look at fixing your Show instance first, if possible.<br></div><div><br></div></div></div></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Nov 2, 2017 at 9:41 AM, Baa <span dir="ltr"><<a href="mailto:aquagnu@gmail.com" target="_blank">aquagnu@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello all!<br>
<br>
I found some errors in the reading of previously shown big and<br>
complex record. Reason is: some of fields are reading wrongly. So, is<br>
there any useful documents how to write `Read` instance correctly<br>
(because I can't find good tutorial in the Web and often hit errors<br>
with `Read` instance)? May be tutorial/examples/any good info...<br>
<br>
<br>
===<br>
Best regards, Paul<br>
______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div>