How to Use Haskell Libraries for Functional Programming
Haskell has a huge library support base in which you can find all sorts of useful functions. Using library code for functional programming is a time saver because libraries usually contain well-constructed and debugged code. The
import function allows you to use external code. The following steps take you through a simple Haskell library usage example:
- Open GHCi, if necessary.
- Type import Data.Char and press Enter.
Note that the prompt changes to Prelude Data.Char> to show that the import is successful. The
Data.Charlibrary contains functions for working with the
Chardata type. You can see a listing of these functions. In this case, the example uses the
ordfunction to convert a character to its ASCII numeric representation.
- Type ord(′a′) and press Enter.
You see the output value of 97.How to Use Haskell Libraries for Functional Programming
You can obtain datasets for Haskell that can be used in functional programming, but first you need to perform a few tasks. The following steps will work for any platform if you have installed Haskell correctly.
- Open a command prompt or Terminal window with administrator privileges.
- Type cabal update and press Enter.
You see the update process start. The cabal utility provides the means to perform updates in Haskell. The first thing you want to do is ensure that your copy of cabal is up to date.How to Use Haskell Libraries for Functional Programming
- Type cabal install Datasets and press Enter.
You see a rather long list of download, install, and configure sequences. All these steps install the Datasets module onto your system.
- Type cabal list Datasets and press Enter.
The cabal utility outputs the installed status of Datasets, along with other information. If you see that Datasets isn’t installed, try the installation again by typing cabal install Datasets –force-reinstalls and pressing Enter instead.
The Boston Housing dataset can be used as an example, so the following steps show how to load a copy of the Boston Housing dataset in Haskell.
- Open GHCi or WinGHCi.
- Type import Numeric.Datasets (getDataset) and press EnterNotice that the prompt changes.
In fact, it will change each time you load a new package. The step loads the
getDatasetfunction, which you need to load the Boston Housing dataset into memory.
- Type import Numeric.Datasets.BostonHousing (bostonHousing) and press Enter.
BostonHousingpackage loads as
bostonHousing. Loading the package doesn’t load the dataset. It provides support for the dataset, but you still need to load the data.
- Type bh <- getDataset bostonHousing and press Enter.
This step loads the Boston Housing dataset into memory as the object
bh. You can now access the data.
- Type print (length bh) and press Enter.
You see an output of