Updated April 10, 2023
Definition of Haskell Array
In Haskell array is used to store the different values, it is immutable in nature, which means once assigned we cannot modify the original one. If we perform any operations on the array then it will always return us to the new array, only accessing elements is fine with the original one. In a functional programming language, we have two main approaches named monolithic and incremental in the coming section we will discuss the two approaches in detail, we can create an array in Haskell by using two ways one is direct initialization and one is mentioned the index and size of the array. In the coming section of the tutorial, we will see the internal working, and implementation in detail for better usage while programming for beginners.
As we know that an array used to store data, we have two approaches to create it and initialize it. Let’s take a look at the syntax for array creation in Haskell for beginners for better understanding see below;
Ix a => (a,a) -> [(a,b)] -> Array a b
As you can see in the above line of syntax, given by the Haskell official documentation, this parameter defines the index and type of array, which we will discuss in the coming part. Let’s take a look at the practice syntax for better understanding see below;
array (index, typr) [(1,"value 1"),(2,"value 2"),(3,"value 2"),(4,"and so on .."))]
As you can see in the above line of practice syntax, this is now pretty much clear how we can define the array in Haskell. We are using the ‘array’ keyword to define it. Now we will see the internal working of the array and usage in detail for better understanding for beginners.
How array works in Haskell?
As we now already know that array is used to store the elements together as a group. We can specify the index as well, inside the array we can store any type of data in an array. Also in Haskell, we have two approaches to create the array named incremental and monolithic. In the incremental approaches, we have one defined function which generates the array for us with a given size but the array would be empty here we can assign it the value later after creation. In the monolithic approach, we can give the index value, array and it will produce us the new array which will be different from the given or original array. Arrays are immutable in nature which means once the array is created we cannot modify it, if we are performing any operation on it then we will always receive a new array not the original one.
Now we will take a closer look at the array signature given by the Haskell and try to understand each passing parameter inside it in detail see below;
signature of the array by Haskell;
Ix a => (a, a) -> [(a,b)] -> Array a b: Here Ix is the library that defines the index for us, by the use of this we can define the instances for integer, string, char, and number, etc. in Haskell. Also, we have a length which is of 5. We can also use the range function which can produce an array for us contenting the elements within this range only. The one signature we are using here is a monolithic approach which takes an index with the array and a value to create the array in Haskell. suppose we want to create an array from 1 to 10, then the index will start from the 1 and goes up to 10. we can give the definition as (1, 10), below we will see one piece of sample code which will use the monolithic approach to create the array in Haskell, by this wit will more clear to us how to mention the parameter inside the monolithic approach see below;
import Data.Array a = array (0,2) [(0,1),(1,5),(2, 10)] main = do print(a)
As you can see in the above lines of code we are trying to create an array in Haskell, so in order to create a monolithic array then we have to include one import statement into our program which is named Data. Array, if we forgot to include this then it will give us an error while creating the array so it is mandatory. After this, we are trying to create the array by passing the required parameter inside it. One is the index and another one is the size of the array, here we have mentioned ‘0’ as the index for the array which means the index will start from 0 and goes up to the side of the array, After this variable, we have assigned the size of the here it is ‘2’ which means it will take 3 pair of the group not more than this if we try to assign the value greater than the size of the array it will throw us an error saying index out of bounds exception. After the index and size, we have given the values which contain the index and the actual value we want to store in the array. In the main module, we have just try to print the array so it will display the whole array values for us.
In this example we are trying to create the array of different size, initiation with the different index by using the Haskell Data, Array import in the program, without it program will it work, This is a sample ex pale for beginners to understand the basic concept of the array in Haskell.
import Data.Array a = array (0,2) [(0,1),(1,5),(2, 10)] b = array (1,3) [(1,10),(2,20),(3, 30)] c = array (2,4) [(2,400),(3,500),(4, 600)] d = array (3,5) [(3,25),(4,26),(5, 27)] e = array (4,6) [(4,90),(5,80),(6, 110)] main = do print("demo to show array certaing in HAskell !!!") print("aray one is ::" , a) print("aray two is ::" ,b) print("aray three is ::" ,c) print("aray four is ::" ,d) print("aray five is ::" ,e)
By the use of an array, we can store our array element inside it, it can be f any number and size. we have seen two approaches for this line is incremental and another one is monolithic as discussed. Arrays are very easy to implement and handle, also they are immutable which means we can’t modify it, only the operation performed will return us a new one but will not modify the existing one.
We hope that this EDUCBA information on “Haskell Array” was beneficial to you. You can view EDUCBA’s recommended articles for more information.