
15 Jan
2010
15 Jan
'10
2:18 p.m.
An absolute beginner as i am, want to use "read" to convert a String in a Int, but I don't know how to protect my ? > >function func from an exception when str is not a number like "12A". How can I do?
Try reads instead. Something like readMaybe s = case reads s of [(i,"")] -> Just i _ -> Nothing Returns "Just" the value parsed in case all input is consumed and Nothing otherwise. A similar version can be made if partial parses are acceptable. Rahul