[Haskell-cafe] Trying to write 'safeFromInteger'

Bulat Ziganshin bulat.ziganshin at gmail.com
Wed Apr 8 05:39:16 EDT 2009


Hello Henning,

Wednesday, April 8, 2009, 1:43:31 AM, you wrote:

> i is Integer, so asTypeOf is not so easy to apply. I propose to write

  safeFromInteger i =
    let mb = maxBound in
    if i > (toInteger mb)
      then Nothing
      else Just (fromInteger i `asTypeOf` mb)

or you can do it opposite way:

  safeFromInteger i =
    let res = fromInteger i in
    if i > (toInteger (maxBound `asTypeOf` res))
      then Nothing
      else Just res


-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Haskell-Cafe mailing list