- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Haskell Program to convert long type variables into int
- Haskell Program to convert int type variables to char
- Haskell Program to convert int type variables to String
- Haskell Program to convert int type variables to double
- Haskell Program to convert double type variables to int
- Golang Program to convert long type variables to int
- Golang Program to convert int type variables to long
- C++ Program to Convert long Type Variables to int
- C++ Program to Convert int Type Variables to long
- Haskell Program to convert string type variables into int
- Swift Program to Convert Char type variables to Int
- Golang Program to convert double type variables to int
- Golang Program to convert int type variables to String
- Swift program to convert double type variables to int
- Haskell Program to convert double type variables to string
