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:
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.
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
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
:m + ...to get access to it.
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)
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)
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
Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know.
Knowledge is the only thing that doubles when you spend it.
I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation