[Haskell-cafe] NaN as Integer value

Daniel Díaz Casanueva dhelta.diaz at gmail.com
Sat Apr 13 19:12:41 CEST 2013


You can always use the Maybe type as a follows:

intDiv :: Integer -> Integer -> Maybe Integer
intDiv _ 0 = Nothing
intDiv n m = Just (div n m)

This allows you to pattern match results of divisions:

example :: Integer -> Integer -> Maybe Integer
example n m =
 case intDiv 4 n of
  Nothing -> Nothing
  Just n' ->
    case intDiv 5 m of
      Nothing -> Nothing
      Just m' -> Just (n' + m')

Or even better using the do notation:

example2 :: Integer -> Integer -> Maybe Integer
example2 n m = do
    n' <- intDiv 4 n
    m' <- intDiv 5 m
    return (n' + m')

Note that example and example2 both do the same thing.

I think this is cleaner solution add NaN as a value to the Integer type.

Good luck,
Daniel Díaz.


On Sat, Apr 13, 2013 at 12:16 PM, Алексей Егоров <electreg at list.ru> wrote:

> Hello haskellers,
>
> is there a reason why Integer doesn't have 'NaN' as value?
> I think it would be very convenient to be able to handle (1 `div` 0) as
> regular NaN value and not as exception.
>
> Thanks.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


-- 
E-mail sent by Daniel Díaz Casanueva

let f x = x in x
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130413/27a4e0fc/attachment.htm>


More information about the Haskell-Cafe mailing list