[Haskell-cafe] testing for exceptions

Darren Grant dedgrant at gmail.com
Sat Oct 31 12:44:39 UTC 2015


You're very close. Take a look at the even clause: Let (k :: Integer), and
consider how the type of the expression producing the error,

  Just (f2Maybe k)

differs from the type of the expression,

  (f2Maybe k)

Cheers,
Darren


On Sat, Oct 31, 2015 at 11:27 PM, Roelof Wobben <r.wobben at home.nl> wrote:

> Op 31-10-2015 om 12:10 schreef Roel van Dijk:
>
> Small nitpick, but I would generally put the "exception" or "error" in the
> Left part of an Either and a correct result in the Right part.
>
> This has some advantages.
> 1 - Right is right as opposed to wrong. Easy to remember mnemonic.
> 2 - It fits neatly with the Monad (Either e) instance.
>
> Roelof, a nice exercise is to first implement your f2 function with the Integer
> -> Maybe Integer type and then with Integer -> Either String Integer.
> If you realize that both Monad Maybe and Monad (Either e) you can use
> almost the same code.
>
> 2015-10-31 12:04 GMT+01:00 Darren Grant <dedgrant at gmail.com>:
>
>> I'd expect most Haskellers to recommend something like,
>>
>>   f2 :: Integer -> Either Integer ErrorString
>>
>> where ErrorString is some specific error value type. (String may suffice
>> for you.)
>>
>> This is a safe general solution, but there are many potentially more
>> specific possibilities that might make your program simpler depending on
>> how this function relates to the context it will be used in.
>>
>> Cheers,
>> Darren
>> On Oct 31, 2015 21:46, "Roelof Wobben" <r.wobben at home.nl> wrote:
>>
>>> Op 31-10-2015 om 11:40 schreef Darren Grant:
>>>
>>>>
>>>> Unfrotunately the answer to this is not simple:
>>>>
>>>>
>>>> http://stackoverflow.com/questions/4243117/how-to-catch-and-ignore-a-call-to-the-error-function
>>>>
>>>> 'error' more or less terminates the program in an unreasonable way.
>>>>
>>>> It would be preferable for f2 to result in a type that can contain the
>>>> error result to be parsed.
>>>>
>>>> Cheers,
>>>> Darren
>>>>
>>>>
>>>>
>>>
>
> Here my try for the Maybe
>
>
> f2Maybe :: Integer -> Maybe Integer
> f2Maybe n
>    | n > 0  = Nothing
>    | n == 0  = Just 1
>    | even n = Just (f2Maybe ( n `div` 2) ^ 2)
>    | odd n  = Just ((f2Maybe ( n `div` 2) ^ 2) * 2)
>
>
> But it will not compile , the clauses for even and odd do not work.
>
> Both maybe and either are not explained in the first 4 chapter of the
> Craft book.
>
> Roelof
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20151031/c6e38a60/attachment.html>


More information about the Haskell-Cafe mailing list