haskell - throwing an error on Read()

Please support our Legacy and Other Languages advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2008
Posts: 29
Reputation: artemis_f is an unknown quantity at this point 
Solved Threads: 0
artemis_f artemis_f is offline Offline
Light Poster

haskell - throwing an error on Read()

 
0
  #1
Apr 1st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 139
Reputation: thoughtcoder is on a distinguished road 
Solved Threads: 12
thoughtcoder thoughtcoder is offline Offline
Junior Poster

Re: haskell - throwing an error on Read()

 
0
  #2
Apr 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 29
Reputation: artemis_f is an unknown quantity at this point 
Solved Threads: 0
artemis_f artemis_f is offline Offline
Light Poster

Re: haskell - throwing an error on Read()

 
0
  #3
Apr 2nd, 2009
Ah thanks! That solves my difficulties!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Legacy and Other Languages Forum
Thread Tools Search this Thread



Tag cloud for Legacy and Other Languages
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC