Updated May 31, 2023
Introduction to Haskell Library
In Haskell, we have so many libraries which have their own importance, and they provide us with various functions which are used to perform various operations when needed. This library comes up with different packages containing a list of conation to perform the logic and operation fatly. There may be some library that needs to be imported while using it in the program. But in Haskell, we have Prelude; this is an implicit library of the Haskell, which is imported by default; there should not be any explicit import statement for this we can use this directly.
How do Haskell Libraries Work?
As we already know that we have so many different libraries present in Haskell with their own importance; we can use the direct or use the import statement to use them in the code.
Here we will see the different libraries available in Haskell for use to perform the different operations as well.
- Platform Libraries in Haskell: In Haskell, we have a platform library that includes support for the network, quick check, and many more. Before releasing this library, they are well tested and preinstalled for use. They provide us with additional packages for this, which have a Haskell Platform library.
- 2010 Libraries in Haskell: This library also provides us various packages which all the Haskell libraries should support; this library provides us with the basic functionality which can be used with the monad, data list, io, and many more.
- Haskell Prelude Library: This is the most important library of Haskell, which is used to perform the more basic operation on the data structures and data type we have in Haskell.
Here we will see the details and various functions of this library with their syntax:
These modules are nothing but contain the required library, which is very much needed to write a basic program in Haskell or in any other programming language.
Here we will see what the functions that are available inside this module are. While using this module, we do not require to import any statement or dependency while using the default functions present inside Haskell’s prelude module.
Let’s see all the methods:
1. abs
This is the function that is available inside the prelude module and can be accessed directly with its name. Basically, this function will return us the absolute value the input passes as the parameter inside it.
Syntax:
print(abs (-1))
2. all
This function from the prelude is used to check whether all the elements from the list or the array satisfy the given condition or not. It always returns us the Boolean value based on the evaluation of the condition. If all the elements satisfy the condition, then it will return true else, it will return False.
Syntax:
all predicate your_list
3. ceiling
This function from predicate is used to return the integer value, which is the next possible approx value for that passing argument.
Syntax:
ceiling your_value
4. Chr
This function from the predicate module is used to get the ASCII value for that particular character between the range of 0 to 255.
Syntax:
chr your integer value between 0 to 255
5. compare
This is a function from the prelude module, which is used to compare the two values which are of the same type. It will return three results based on the evaluation of the comparison, i.e., EQ, GT, LT. All have some meaning, EQ if the two values are equal, LT if the first value is less than or equal to the second value passed, GT if the first value is greater than the second value passed.
Syntax:
compare value1 value2
6. concat
This function from the prelude is used to concat the two lists together. It is very easy to use and implement, as well.
Syntax:
concat [value to be concate]
7. div
This function from the prelude module is used to calculate the integer division of the passing param.
Syntax:
val1 `div` val2
8. drop
This is a function from the prelude module, which is used to drop the specified elements from the list or array.
Syntax:
drop yournumber [list] or varibale
Example of Haskell Library
Given below is the example of Haskell Library:
Basic example to show usage of Haskell library, with how to use their functions in detail.
Code:
main = do
print("Demo to show library in Haskell with its function !!")
let val1 = 100
let val2 = -10
let val3 = -3
let val4 = 80
let val5 = -90
let val6 = 8
let val7 = 15
print("Using the abs function to!!")
let result1 = abs val1
let result2 = abs val2
let result3 = abs val3
let result4 = abs val4
let result5 = abs val5
let result6 = abs val6
let result7 = abs val7
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)
print("Result evens ::", result7)
Output:
Conclusion
In Haskell or in any other programming language, we have so many libraries that are inbuilt, or we can use an external library for better performance. But in Haskell, we have the Prelude module, which contains all the necessary things required to write a program in Haskell; also, these libraries are very easy to use, readable, and easy to understand by developers.
Recommended Articles
We hope that this EDUCBA information on “Haskell Library” was beneficial to you. You can view EDUCBA’s recommended articles for more information.