safeDiv :: (Exception e, Integral a) =>
           a -> a -> Either e a
safeDiv x y = unsafePerformIO . try . evaluate $ div x y

I just want to know, from a theoretical point of view,
whether this 'safeDiv' in above definition is the same as

safeDiv' :: (Exception e, Integral a) =>
            a -> a -> Either e a
safeDiv' _ 0 = Left e
safeDiv' x y = Right $ div x y

No. Firstly, safeDiv' doesn't compile!-) Then, if you replace
'e' by 'DivideByZero' and adjust the types:

   *Main> safeDiv 1 (throw Overflow)
   Left arithmetic overflow
   *Main> safeDiv' 1 (throw Overflow)
   *** Exception: arithmetic overflow

Try ':info ArithException' for more in the same group. You
could use other functions in Control.Exceptions to get more
control about which exceptions you want to handle and how,
but so far, there is no indication that 'unsafePerformIO' is
the right hammer to use here..

Claus

-- unsagePerformIO: some things are just not wise to do

_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to