Updated May 31, 2023
Introduction to Haskell vector
Haskell vector is used to deal with arrays in Haskell, it is a library which helps us to handle the array in Haskell. Vector in Haskell is not in built we have to include the library in order to use this while programming in Haskell. By the help of vector, we can have high performance as well, vector provide us high performance by the use of loop fusion, we mainly have two data types for vector one is unboxed array and another one is boxed array. Vector API provide us several function to handle the array in Haskell efficiently.
Syntax:
To use vector we would require to have vector API in place, let’s have a look at its syntax how we can use this while programming.
let variable_name = fromList['val1', 'val2', 'val3, '' so on]
As you can see in the above line of syntax it is very easy to use and implement, we are using ‘fromList’ function to create the vector.
Let’s take a practice syntax:
let demo = fromList[10, 20, 30, ....]
How vector Work in Haskell?
As we already know that vector is used to handle the array in Haskell, they also provide us high performance as well. Vector in Haskell handle mostly two types such us boxed array and unboxed array, we have to use the vector API to use vector in Haskell programming language.
Below see the following steps to include vector api in the Haskell program:
Step 1: First we have to install the vector API in order to use this while programming.
We have below command to install the vector API in the program:
Example:
cabal install vector
After execution of this command you will see several packages get installed into the application. After this only we will be able to use the vector inside our project.
Step 2: Once the package is successfully installed then we have to import it inside our program, to import the package we have the following statement.
import qualified Data.Vector as V
We first have the use the import keyword followed by the name of the package we want to use in our program. After that we have given it name as any suitable variable to refer it inside the program. Which will make it handy to use it anywhere.
Step 3: In the third step we can import one optional package inside our program if we wish to use the unboxed array in vectors for this we may require to import the different package for this.
Example:
import qualified Data.Vector.Unboxed as V
To import this package we have used unboxed from vector library, this package is used to handle the unboxed array in Haskell, we can also handle boxed array by the use of above package in the second step.
Step 4: Now in this step we can now create the vector from the existing list like we create in list API in Haskell, in this step we will see how we can create the vector.
Example:
let demo = fromList [10, 20, 30 40, 50]
Step 5: We can also create empty vector like we create list in Haskell.
Example:
empty
Step 6: We can also create singleton vector in Haskell like we create in list.
Example:
singleton ‘value’
In this way we have several ways to create vector in Haskell, it works in the same way like list API in Haskell.
But we have to remember some of the points:
- It is not an inbuilt function of Haskell, if we want to use this then we have to install it. After this only we will be able to use this and create vector in Haskell.
- It is majorly used to handle the boxed and unboxed array data types in Haskell.
- Vector provide us the high performance, but the use of loop fusion.
Example
Given below is the example mentioned:
In this example we are trying to use the vector ai and trying to create the vector from the ‘fromList’ function like list API. This is very easy to use and handle by the developers as well.
Code:
import qualified Data.Vector as V
set1 = fromList ['a'..'z']
set2 = fromList [10, 20, 30, 40, 50, 60]
set3 = fromList ["Hello", "world", "bye", "enjoy", "moment"]
set4 = fromList [1, 2, 3, 4, 5, 6, 7]
set5 = fromList [400, 200, 100, 500, 900, 800]
set6 = fromList ["value 1", "value 2", "value 3", "value 3", "value 4"]
set7 = fromList [1.1, 2.2, 3.3, 4.4, 5.5, 6.6]
main = do
print("Demo to show set in Haskell !!")
print("Printing the result !!")
print("first set is :::", set1)
print("second set is :::", set2)
print("third is :::", set3)
print("fourth set is :::", set4)
print("fifth set is :::", set5)
print("sixth set is :::", set6)
print("seventh set is :::", set7)
Output:
Conclusion
Vector are easy to use, and mainly designed to handle the array. Provide us high performed, they act in the same way like list API in Haskell. But to use them we have to have import packages into place, at start of the program.
Recommended Articles
We hope that this EDUCBA information on “Haskell vector” was beneficial to you. You can view EDUCBA’s recommended articles for more information.