Updated May 31, 2023
Introduction to Haskell read
In Haskell read function is used to deal with strings, if you want to parse any string to any particular type than we can use read function from the Haskell programming. In Haskell read function is the in built function, that means we do not require to include or install any dependency for it, we can use it directly. In short read function is used for parsing of string values in Haskell, like other programming language, not only single value we can parse the array as well using read function.
Syntax:
read "your string here ":: your type you want it to cast to ..
As you can see in the above line of syntax it is pretty much clear, for this we have used the ‘read’ keyword only followed by the string we want to parse.
Example:
read “1234”:: Float
From the above line of code it clears how to use this.
How read Function Works in Haskell?
The read function in Haskell is very easy to use and handle, and it helps the developers to convert the string into desired type available in Haskell. Also its syntax is very much clear and only requires us to use the ‘read’ keyword and the string we want to parse.
Here we will see the basic signature of the read function given by the Haskell official website.
Method Signature:
- Read a => String -> a: This is basic signature of read method by the documentation , it says by the use of it we can convert of parse the valid string to the any particular type available in Haskell. Sometime when the string is not correct it can throw exception at runtime. It not only parse the single value but it can also parse the array at once which contain valid string, else it will throw exception. It is useful when you want to perform any mathematical operation on the string, so on order to do it we may want string to be cast in any number format to proceed further.
Example:
Code:
main = do
let demo = read "100" :: Int
print(demo)
As you can see in the above sample piece of code we are trying to parse the string to Int. In the first line we have the main function which is responsible to start the execution of the Haskell program, In the next step we are defining one variable called ‘demo’ which will hold the parse value of string, for this we are calling ‘read’ function here, let’s take an closer look at the working and its usage.
- First we use the ‘read’ keyword followed by the string that we want to convert to any particular type.
- String should be an valid string that can be cast to any number type. it should not be invalid string otherwise it will throw exception like any other programming language.
- After defining your string we can give the number type in which we want to cast or string.
So this function is very easy to use, handle and maintain as well.
Examples
Given below are the examples of Haskell read:
Example #1
In this example we are trying to parse the sting value to int, by using the read function Haskell.
Code:
main = do
print("Demo to show usages of read function in haskell !!")
print("calling read function here")
let val1 = read "100" :: Int
let val2 = read "200" :: Int
let val3 = read "300" :: Int
let val4 = read "10" :: Int
let val5 = read "20" :: Int
let val6 = read "30" :: Int
print("printing the result here ..")
print("result one is ::", val1)
print("result two is ::", val2)
print("result three is ::", val3)
print("result four is ::", val4)
print("result five is ::", val5)
print("result six is ::", val6)
Output:
Example #2
In this example we are trying to parse the string to double value in Haskell, by using the read function also it is in built we have not include any dependency for it.
Code:
main = do
print("Demo to show usages of read function in haskell !!")
print("calling read function here")
let val1= "12.3"
let val2= "100.4"
let val3= "30.5"
let val4= "10.4"
let val5= "34.8"
let val6= "90.3"
let result1 = read val1 :: Double
let result2 = read val1 :: Double
let result3 = read val1 :: Double
let result4 = read val1 :: Double
let result5 = read val1 :: Double
let result6 = read val1 :: Double
print("printing the result here ..")
print("result one is ::", result1)
print("result two is ::", result2)
print("result three is ::", result3)
print("result four is ::", result4)
print("result five is ::", result5)
print("result six is ::", result6)
Output:
Conclusion
By the use of read function we can convert our string to number, also if we want to perform any operations on string we can use this read function to first parse it as number then only we can perform calculations on it, otherwise calculation will be wrong also.
Recommended Articles
We hope that this EDUCBA information on “Haskell read” was beneficial to you. You can view EDUCBA’s recommended articles for more information.