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.Char
library contains functions for working with theChar
data type. You can see a listing of these functions. In this case, the example uses theord
function 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
- 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.
- 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
getDataset
function, which you need to load the Boston Housing dataset into memory. - Type import Numeric.Datasets.BostonHousing (bostonHousing) and press Enter.
The
BostonHousing
package loads asbostonHousing
. 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
506
.