### 3 Tutorial 2: Studying functions in Haskell

#### 3.1 Worked example

We can use Haskell to study the behaviour of mathematical functions. The function $f\left(x\right)$ is defined as

and plotted in Figure 1.

- 1.
- To study the function, we need to define it as a function. Create a module in a file
NumTutorial.hs, with the following definition
`module NumTutorial where`

f :: Double −> Double

f x = 2*x**2 − 4*x - 2.
- In this exercise, we will use the easyplot library. We install it with the following commands
before we start GHCi.
`cabal update`

cabal install easyplot - 3.
`easyplot`

depends on gnuplot, which you also have to install. If you run Debian/Ubuntu, try the following.`apt−get install gnuplot`

Otherwise please google for installation instructions for your OS.

- 4.
- Open GHCi
`ghci`

- 5.
- Load the module
`:l NumTutorial`

- 6.
- Test the function
`f`

`f 0`

f 1

f (−1)Do the results look reasonable?

- 7.
- We import the EasyPlot library so that we can use it
`import Graphics.EasyPlot`

You will see that the prompt changes, to indicate loaded modules.

- 8.
- The following command plots the function
`f`

.`plot X11 ( Function2D [] [] f )`

The second argument to

`plot`

is a data set. The`Function2D`

function creates such a data set from a function.Note If you run Windows you need to replace

`X11`

with`Windows`

(unless you have an X server running). There is a similar`Aqua`

option for Mac OS, but Mac ofthen has an X server as well.If the plot window closes before you manage to see it, try the following variant instead (Windows):

`plot’ [Interactive] Windows ( Function2D [] [] f )`

- 9.
- The two empty brackets in the arguments for
`Function2D`

are lists of options. We can add a title and restrict the range, as follows:`:set +m`

plot X11 ( Function2D

[Title "The f function"]

[Range (−20) (20)] f )The

`:set +m`

command is necessary to allow one statement to span multiple lines. The plot statement has been split over three lines just to make it easier to read. - 10.
- The
`X11`

argument to`plot`

says that the plot be shown in an X11 window. It is possible to plot to file, as follows:`:set +m`

plot (PDF "fplot.pdf") ( Function2D

[Title "The f function"]

[Range (−20) (20)] f ) - 11.
- Leave GHCi (use the
`:q`

command). - 12.
- Open the PDF plot, using
`okular fplot.pdf &`

If you want to learn more about EasyPlot, you find documentation on Hackage.

#### 3.2 Practice Problem

Let’s study a second function $g\left(x\right)$. A plot is shown in Figure 2, and the definition is

Please implement and plot this function in Haskell, as follows:

- 1.
- Add a definition for a function
`g`

in the file NumTutorial.hs. - 2.
- Open GHCi and load NumTutorial.
- 3.
- Evaluate $g\left(0\right)$,
$g\left(1\right)$,
and $g\left(-1\right)$
to test the function
`g`

. Do the results look reasonable? - 4.
- Import
`Graphics.EasyPlot`

- 5.
- Plot the function
`g`

on the range $-10\ge x\ge 10$ in a window. Give the curve a meaningful title. - 6.
- Plot the
`g`

function to a PDF file (gplot.pdf). - 7.
- Leave GHCi (use the
`:q`

command) and open the PDF plot in okular (or another PDF viewer).