w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How do I turn an Integer into an Int in Haskell

Just use the fromInteger function:

fromInteger :: Num a => Integer -> a

You'd use it in GHCi like

> fromInteger (1 :: Integer) :: Int
1

But beware that there's some interesting behavior if you go beyond the bounds of an Int:

> let x = (fromIntegral (maxBound :: Int) :: Integer) + 1
> x
2147483648
> fromInteger x :: Int
-2147483648




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.