Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Int32 to Int in Haskell

Tags:

haskell

What function, or series of functions, can convert an Int32 to an Int in Haskell? I know that HashTable.hashString can convert to an Int32, but I need a function that can convert the other way.

like image 232
Mike Avatar asked Feb 02 '11 21:02

Mike


3 Answers

ghci> import Data.Int
ghci> fromIntegral (5 :: Int32) :: Int
5
ghci> fromIntegral (5 :: Int) :: Int32
5
like image 120
ephemient Avatar answered Sep 29 '22 15:09

ephemient


Stop. Hoogle time!...Sadly, Hoogle fails to answer the question well this time. Skipping to the answer...

fromIntegral :: (Integral a, Num b) => a -> b

(docs at hackage.haskell.org)

Note the type signature of fromIntegral indicates that it will turn any Integral into any Num. Since Int32 has a declared instance of Integral, and Int has a declared instance of Num, the fromIntegral function will do the job.

like image 30
Dan Burton Avatar answered Sep 29 '22 16:09

Dan Burton


fromIntegral will convert from Int32 to Int or the other way around.

like image 45
porges Avatar answered Sep 29 '22 16:09

porges