Updated April 10, 2023
Introduction to Haskell list
In Haskell list is used to store the elements, and these elements are homogenous in nature which simply means only one type. Inside the list, we can store the list of elements that are of the same type. we can store any data type inside the list. Also, it provides us some functions by the use of it we can modify the elements of the list, also we came to merge two lists together by using the functions available in the list of Haskell. In the comings section of the tutorial, we will see the internal working of the list with its implementation and usage while programming in Haskell.
Syntax
As discussed it is used to store the elements inside it, let’s take a closer look at the syntax of the list for a better understanding of it to see below;
let varibale_name = [value1, value2, value3 ..]
As you can see in the above line of syntax we are creating a list, it is very easy to use and handle by the developer with clean and readable syntax. Let’s take a practice syntax for beginners for a better understanding of it see below;
Example:
let demo = ["val1", "val2", "val3", "so on"]
from the above line of practice syntax, it is pretty much clear to us, in the coming section of the tutorial we will see the internal working and how we can modify the elements of the list by using a function in detail for beginners to start using it while programming.
How does the list work in Haskell?
As now we already know that list is used to store the elements inside it, it is a data structure that is responsible to store the homogenous type of data inside it, we can store any type of data inside the list which can be string, number, character and so on. We also have some functions of the list by the use of it we can perform operations on the list variable. First, we will have a closer look at the syntax and signature of the list see below;
Signature:
let variable = [your values]
We can simply create a list in Haskell like any other variable, we just used [] ‘square braces’ to initialize the list elements in Haskell. Also remember it will store only one type of element inside it. Now we will have to look at the function that we can use with list variable to perform the operation which is described as below;
1. length: by the use of this function we can get the length of the list variable in Haskell, it will simply return us the length in the form of the integer value. It shows us how many elements are present inside the array or list. Below see the syntax to use the function with the list.
Example:
length list_name
We can just pass the list variable after the length function in Haskell.
2. head: This function will return us the first elements of the list, which is present at the starting potion. Below see the syntax to use the function with the list in Haskell ;
Example:
head list_name
We can just pass the list variable after the head function in Haskell.
3. tail: This function will return us the remaining elements except for the head of the list. Below see the syntax to use the function with the list.
Example:
tail list_name
We can just pass the list variable after the tail function in Haskell.
4. last: This function will return us the last elements present in the list. Below see the syntax to use the function with the list.
Example:
last list_name
We can just pass the list variable after the last function in Haskell.
5. reverse: This function will return us the list in the reverse order of the original list. Below see the syntax to use the function with the list.
Example:
reverse list_name
We can just pass the list variable after the reverse function in Haskell.
6. init: This function will return us the whole list except the last element from the list. Below see the syntax to use the function with the list in Haskell ;
Example:
inti list_name
We can just pass the list variable after the init function in Haskell.
Examples
Here are the following examples mention below
Example #1
In this example, we just certain a list and trying to print it. We have created several list variables with every type of data inside them. This is a sample example for beginners to understand it better.
Code:
main = do
print("Demo to show list in Haskell !!")
let list1 = [100, 50, 235, 167, 345, 909, 675, 20]
let list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
let list3 = [1.1, 2.2, 3.3, 4.4, 5.5, 6.6]
let list4 = [5, 10, 15, 20, 15, 30, 35, 40]
let list5 = [123.4, 567.9, 56.0, 3.9, 76.9]
print("printing list element !!")
print("list one is ::" , list1)
print("list two is ::" , list2)
print("list three is ::" , list3)
print("list four is ::" , list4)
print("list five is ::" , list5)
Output:
Example #2
Example to show the merging of two list.
Code:
main = do
print("Demo to show list in Haskell !!")
let list1 = [100, 50, 235, 167, 345, 909, 675, 20]
let list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
let list3 = [1.1, 2.2, 3.3, 4.4, 5.5, 6.6]
let list4 = [5, 10, 15, 20, 15, 30, 35, 40]
let list5 = ["hello", "world", "we ", "are ", "merging"]
let list6 = ["list 2", "elements ", "are ", "here ", "success"]
print("trying to merge the list objects !!")
let result1 = list1 ++ list2
let result2 = list3 ++ list4
let result3 = list5 ++ list6
print("printing the result after merge !!")
print("list one is ::", result1)
print("list two is ::", result2)
print("list three is ::", result3)
Output:
Conclusion
By the use of this list, we can store any number of elements inside it, which is required when we have a lot of data for the application. Also, we have so many functions available which can be used with list variables to get the value and perform desired operations. The list is very easy to use, readable, and easy to maintain as well.
Recommended Articles
We hope that this EDUCBA information on “Haskell list” was beneficial to you. You can view EDUCBA’s recommended articles for more information.