Lesson 15 of 16

Module System

Module System

Haskell's standard library is organized into modules. By default, only Prelude is imported. To use functions from other modules, you need an import statement at the top of your file.

Basic Imports

Import an entire module to bring all its exports into scope:

import Data.Char
import Data.List

Now you can use toUpper, sort, nub, etc. directly.

Importing Specific Functions

To avoid cluttering your namespace, import only what you need:

import Data.Char (toUpper, isAlpha)
import Data.List (sort, nub)

Qualified Imports

When two modules export functions with the same name, use qualified imports:

import qualified Data.Map as Map

myMap = Map.fromList [("a", 1), ("b", 2)]
value = Map.lookup "a" myMap  -- Just 1

With qualified, you must always use the prefix: Map.lookup, not just lookup.

Common Modules

ModuleKey Functions
Data.ChartoUpper, toLower, isAlpha, isDigit
Data.Listsort, nub, group, intercalate
Data.MapfromList, lookup, insert, member
Data.MaybefromMaybe, isJust, isNothing

Your Task

  1. Import Data.Char (specifically toUpper and isAlpha)
  2. Import Data.List (specifically sort and intercalate)
  3. Define shout that converts a string to uppercase using map toUpper
  4. Define onlyLetters that filters a string to only alphabetic characters using filter isAlpha
  5. Print shout "hello"
  6. Print onlyLetters "h3ll0 w0rld"
  7. Print sort [3, 1, 4, 1, 5]
  8. Print intercalate ", " ["one", "two", "three"]
Haskell loading...
Loading...
Click "Run" to execute your code.