Functional Programming For Dummies
Book image
Explore Book Buy On Amazon
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:
  1. Open GHCi, if necessary.
  2. 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 the Char data type. You can see a listing of these functions. In this case, the example uses the ord function to convert a character to its ASCII numeric representation.

  3. 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.
  1. Open a command prompt or Terminal window with administrator privileges.
  2. 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

  3. 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.

  4. 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.
  1. Open GHCi or WinGHCi.
  2. 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.

  3. Type import Numeric.Datasets.BostonHousing (bostonHousing) and press Enter.

    The BostonHousing package 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.

  4. 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.

  5. Type print (length bh) and press Enter.

    You see an output of 506.

About This Article

This article is from the book:

About the book author:

John Paul Mueller has written more than 100 books and 600+ articles on everything from networking and home security to database management and heads-down programming. His technical editing talents have helped more than 70 authors refine and polish their manuscripts. John's books address every level of skill from beginning to advanced.

This article can be found in the category: