Introduction to Haskell prelude
In Haskell, we have one default module which is imported in the Haskell program by default. We do not require to do it manually through any import statement because it is available by default. This module is called a prelude in Haskell, it is a basic module that contains so many things inside it, which is useful to write any program in Haskell. In this tutorial, we will discuss the prelude internal working and how we can use this inside our Haskell program. Also, we will see how to do this explicitly in our program.
As discussed this is not a function that has some syntax rather it is a module or we can say basic module which is required to program in Haskell. let’s see its explicit syntax given by the Haskell official documentation see below;
As you can see in the above line of syntax we are importing the prelude module explicitly in our program. We have just used the import keyword followed by the Prelude, this will include the module inside our program. But it is not required to do because this module is available in Haskell by default.
In the coming section of the tutorial we will see what are the data types and functions available inside this module that can be used while programming in Haskell, this will give a basic understanding of the prelude module to beginners.
How to use prelude in Haskell?
As of now we already know that prelude is a basic module available in the Haskell programming language like we have so many different modules for different programming languages. 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. In this section, we will see what are the functions that are available inside this module. while using this module we do not require to import any statement or dependency while using the default functions present inside the prelude module of Haskell. Let’s get started with all the methods one by one see below;
1) abs: This is the function that is available inside the prelude module and can be accessed directly with the name of it. Basically, this function will return us the absolute value the input passes as the parameter inside it. Let’s have a look at the syntax of it for better understanding see below;
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. Let’s have a look at the syntax of it for better understanding see below;
all predicate your_list
3) ceiling: This function from predicate used to return the integer value which is the next possible approx value for that passing argument. Let’s have a look at the syntax of it for better understanding see below;
4) chr: This function forms the predicate module is used to get the basic value for that particular character between the range of 0 to 255. Let’s have a look at the syntax of it for better understanding see below;
chr your interger value betwwen 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 lets discuss, 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. Let’s have a look at the syntax of it for better understanding see below;
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. Let’s have a look at the syntax of it for better understanding see below;
concat [value to be concate]
7) div: This function from the prelude module is used to calculate the integer division of the passing param. Let’s have a look at the syntax of it for better understanding see below;
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. Let’s have a look at the syntax of it for better understanding see below;
drop yournumber [list] or varibale
1) In this example we are trying to show the prelude functions available in Haskell we are using the abs function here. also, we have not imported anything to use because it is a default module to Haskell.
main = do
print("Demo to show predle basic module in Haskeel 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)
The prelude module is very useful and important to perform any basic operations in the Haskell program that we have discussed already in the tutorial. Also, this is a default module so we do not require to follow any steps to use this while programming in Haskell, simple to use for beginners and understand.
This is a guide to Haskell prelude. Here we discuss the working and how we can use this inside our Haskell program along with the example and output. You may also look at the following article to learn more –