Haskell Program to convert int type variables to long


In Haskell, we can use internal functions like fromIntegral intToLong and toInteger function and toEnum to convert int type variable to long. In the first example, we are going to use (let longVar = fromIntegral intVar :: Int64) and in the second example, we are going to use (intToLong = toEnum) function.

Algorithm

  • Step 1 − The Data.Int module is imported.

  • Step 2 − The program execution will be started from main function. The main() function has whole control of the program.

  • Step 3 − The variable named, “intVar” is being initialized. It will hold the Int type variable that is to be converted to respective long value.

  • Step 4 − Use the internal function and pass intVar to it.

  • Step 5 − The resultant long value is printed to the console using ‘putStrLn’ statement after the function is called.

Example 1

In this example, the function is defined with the help of fromIntegral function to convert the int type variables to long.

import Data.Int (Int64)

main :: IO ()
main = do
   let intVar = 123456789
   let longVar = fromIntegral intVar :: Int64
   putStrLn $ "The integer value is " ++ show intVar ++ "."
   putStrLn $ "The corresponding long value is " ++ show longVar ++ "."

Output

The integer value is 123456789.
The corresponding long value is 123456789.

Example 2

In this example, the function is defined with the help of fromIntegral and intToLong function to convert the int type variables to long.

import Data.Int (Int64)

intToLong :: Int -> Int64
intToLong = fromIntegral

main :: IO ()
main = do
   let intVar = 123456789
   let longVar = intToLong intVar
   putStrLn $ "The integer value is " ++ show intVar ++ "."
   putStrLn $ "The corresponding long value is " ++ show longVar ++ "."

Output

The integer value is 123456789.
The corresponding long value is 123456789.

Example 3

In this example, the function is defined with the help of fromIntegral and toInteger function to convert the int type variables to long.

import Data.Int (Int64)

main :: IO ()
main = do
   let intVar = 123456789
   let longVar = fromInteger $ toInteger intVar :: Int64
   putStrLn $ "The integer value is " ++ show intVar ++ "."
   putStrLn $ "The corresponding long value is " ++ show longVar ++ "."

Output

The integer value is 123456789.
The corresponding long value is 123456789.

Example 4

In this example, the function is defined with the help of toEnum function to convert the int type variables to long.

import Data.Int (Int64)

intToLong :: Int -> Int64
intToLong = toEnum

main :: IO ()
main = do
   let intVar = 123456789
   let longVar = intToLong intVar
   putStrLn $ "The integer value is " ++ show intVar ++ "."
   putStrLn $ "The corresponding long value is " ++ show longVar ++ "."

Output

The integer value is 123456789.
The corresponding long value is 123456789.

Conclusion

In Haskell, an Int is a fixed-size integer type with a platform-dependent size, while a Data.Int.Int64 is a 64-bit integer type. Converting an Int variable to a Data.Int.Int64 variable involves converting the variable to a larger integer type that can accommodate the larger range of values. In Haskell, an Int type variables is converted to long using fromIntegral function along with intToLong and toInteger function and also by using toEnum function.

Updated on: 13-Mar-2023

177 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements