What is modules

A Haskell module is a collection of related functions, types and typeclasses.

A Haskell program is a collection of modules where the main module loads up the other modules and then uses the functions defined in them to do something.

Having code split up into several modules has several advantages:

  • if a module is generic enough, the functions it exports can be used for many different programming tasks
  • if your own code is separated into self-contained modules which don’t rely on each other too much (we also say they are loosely coupled), you can reuse (some of) them separately later on
  • splitting code into several parts, each of which has a specific purpose, makes writing code more manageable

The Haskell standard library is split into modules. Each of them contains functions and types that are somehow related and serve a common purpose.

There’s a module for manipulating lists, one for manipulating sets, a module for concurrent programming, a module for dealing with complex numbers etc

All the functions, types and typeclasses that we’ve dealt with so far were part of the Prelude module, which is imported by default. In this set of notes and the following one, we’re going to examine a few useful modules and the functions that they make available.

Importing modules

But first, we’re going to see how to import modules. The syntax for importing modules in a Haskell script is:


import < moduleName>

This must be done before defining any functions, so imports are usually done at the top of the file. We can import several modules in one script, we just have to put each import statement on a separate line.


import < moduleName1>
import < moduleName2>
import < moduleName3>
. . .

For example, let’s import the Data.List module, which makes available many useful functions for working with lists.


ghci 1> : show imports
import Prelude – implicit


ghci 2> import Data.List

ghci 3> : show imports
import Prelude – implicit import Data.List

When we do import Data.List, all the functions that Data.List exports become available in the global namespace, meaning that we can call them from wherever in the script.

In ghci, we can also import modules by running :m + Data.List.

If we want to load up the names from several modules inside GHCI, we don’t have to do :m + ... several times, we can just load up several modules at once: :m + Data.List Data.Map Data.Set

Note that if we’ve loaded a script in ghci that already imports a module, we don’t need to use :m + ... to get access to it.

Importing functions from modules :

If we just need a couple of functions from a module, we can selectively import just those functions. If we want to import only the nub and sort functions from Data.List, we do this:


import Data.List (nub,sort)

Importing modules without certain functions :

We can also choose to import all of the functions of a module except a few select ones – that’s often useful when several modules export functions with the same name and we want to avoid name conflicts.

For example, suppose we already have our own function that’s called nub and we want to import all the functions from Data.List except the nub function. Then we do the following:


import Data.List hiding (nub)

Qualified imports :

Another way of dealing with name clashes is to do qualified imports. For example, the Data.Map module, which offers a data structure for looking up values by key, exports a bunch of functions with the same name as Prelude functions, like filter or null.

So when we import Data.Map and then call filter, Haskell won’t know which function to use. Here’s how we solve this:


import qualified Data.Map

This makes it so that if we want to reference Data.Map’s filter function, we have to do Data.Map.filter, whereas just filter still refers to the normal filter we all know and love.

But typing out Data.Map in front of every function from that module is kind of tedious. That’s why we can rename the qualified import to something shorter:


import qualified Data.Map as M

To reference Data.Map’s filter function, we just use M.filter

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : I am starting online training course for Protractor with Typescript from 15th November 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn then you can join the course by sending email to chercher.tech@gmail.com

or Register below


Subscribe
 
Join My Facebook Group
Join Group