943,733 Members | Top Members by Rank

Ad:
Apr 1st, 2009
0

haskell - throwing an error on Read()

Expand Post »
I know how to throw an error in Haskell however I don't know how to do it for the read function. When I do something like this:

-- find the number on the second line of file/second list of strings
getNo :: String->Int
getNo inputBigString = read((listOfLines inputBigString) !! 1)

I want to specify like if the line you are trying to convert to an integer is empty/ or you can't convert it to an Integer then let me specify what error message to specify. At the moment if I input a file where the second line is empty it says
"Program error: Prelude.read: no parse"
but I want to be able to specify what error message should be shown myself - how can I accomplish this?

Thanks
Reputation Points: 10
Solved Threads: 0
Light Poster
artemis_f is offline Offline
30 posts
since Aug 2008
Apr 1st, 2009
0

Re: haskell - throwing an error on Read()

Use reads.
> reads " 234 blah" :: [(Integer, String)]
[(234," blah")]
> reads "blah" :: [(Integer, String)]
[]
It returns a list of the different ways the desired thing can be parsed. For all standard types, this list is of length 1 or 0.
Last edited by thoughtcoder; Apr 1st, 2009 at 9:42 am.
Reputation Points: 231
Solved Threads: 12
Junior Poster
thoughtcoder is offline Offline
139 posts
since Mar 2009
Apr 2nd, 2009
0

Re: haskell - throwing an error on Read()

Ah thanks! That solves my difficulties!
Reputation Points: 10
Solved Threads: 0
Light Poster
artemis_f is offline Offline
30 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Legacy and Other Languages Forum Timeline: Urgent help needed here.
Next Thread in Legacy and Other Languages Forum Timeline: haskell - making a tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC