{"id":1002043,"date":"2009-01-20T23:13:54","date_gmt":"2009-01-21T04:13:54","guid":{"rendered":"http:\/\/www.elharo.com\/blog\/?p=1002043"},"modified":"2009-01-20T23:13:54","modified_gmt":"2009-01-21T04:13:54","slug":"asint_either","status":"publish","type":"post","link":"https:\/\/www.elharo.com\/blog\/software-development\/haskell\/2009\/01\/20\/asint_either\/","title":{"rendered":"asInt_either"},"content":{"rendered":"<p><cite><a href=\"http:\/\/book.realworldhaskell.org\/read\/functional-programming.html\">Real World Haskell<\/a><\/cite>, Exercise 4, p. 98:<\/p>\n<p>The <code>asInt_fold<\/code> function uses <code>error<\/code>, so its callers cannot handle errors. Rewrite it to fix this problem.<br \/>\n<!--more--><\/p>\n<pre>-- file: ch04\/IntParse.hs\r\nimport Data.Char (digitToInt) \r\n\r\nasInt :: String -> Int\r\nasInt ('-':xs) = -1 * asInt xs\r\nasInt xs = foldl step 0 xs\r\n  where step acc x = acc * 10 + safeDigitToInt x\r\n\r\n\r\nsafeDigitToInt :: Char -> Int\r\nsafeDigitToInt x =  if x >= '0' && x &lt;= '9'\r\n                      then digitToInt x\r\n                      else error \"Non-digit\"\r\n\r\nasInt_either :: String -> Ei\r\nasInt_either xs = if onlyHasDigits xs\r\n                  then Main.Right (asInt xs)\r\n                  else Main.Left (\"non-digit '\" ++ ((firstNonDigit xs) : \"'\"))\r\n\r\nonlyHasDigits :: String -> Bool\r\nonlyHasDigits [] = True\r\nonlyHasDigits (x:xs) = if x >= '0' && x &lt;= '9'\r\n                      then onlyHasDigits xs\r\n                      else False\r\n\r\nfirstNonDigit :: String -> Char\r\nfirstNonDigit [] = '\\0'\r\nfirstNonDigit (x:xs) = if x &lt;= '0' || x >= '9'\r\n                      then x\r\n                      else firstNonDigit xs\r\n\r\ndata Ei = Right Int \r\n          | Left String\r\n        deriving (Show)<\/pre>\n<p>Doubtless this could be much mproved by someone who knows what they&#8217;re doing. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Real World Haskell, Exercise 4, p. 98: The asInt_fold function uses error, so its callers cannot handle errors. Rewrite it to fix this problem.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74],"tags":[],"class_list":["post-1002043","post","type-post","status-publish","format-standard","hentry","category-haskell"],"_links":{"self":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts\/1002043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/comments?post=1002043"}],"version-history":[{"count":2,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts\/1002043\/revisions"}],"predecessor-version":[{"id":1002045,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts\/1002043\/revisions\/1002045"}],"wp:attachment":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/media?parent=1002043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/categories?post=1002043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/tags?post=1002043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}