0% found this document useful (0 votes)
2 views11 pages

3-2-Module

The document discusses the concept of modules in Haskell, highlighting their importance for organizing larger programs, enabling code reuse, and improving code management. It covers how to import modules, including selective imports and qualified imports, as well as defining modules and their hierarchical structure. Examples are provided to illustrate the use of modules, such as Data.List and Data.Map.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

3-2-Module

The document discusses the concept of modules in Haskell, highlighting their importance for organizing larger programs, enabling code reuse, and improving code management. It covers how to import modules, including selective imports and qualified imports, as well as defining modules and their hierarchical structure. Examples are provided to illustrate the use of modules, such as Data.List and Data.Map.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CMPT383 Comparative Programming Languages

Lecture 3-2: Modules

Yuepeng Wang
Spring 2025

1
Overview
• Importing modules

• Defining modules

2
Modules
• We have been mostly writing functions so far

• Larger Haskell programs need to be more organized

• A Haskell module is essentially a file that defines some functions, types,


and type classes

• A Haskell program typically consists of a set of modules

3
Advantages of Modules
• Code reuse

• For a general-purpose module (e.g., list operations), the functions it


exports can be used in different programs

• Code management

• The code is more manageable when you split it into several parts

4
Importing Modules
• Syntax: import ModuleName

• Import all that is exported by ModuleName

• The imports must be done before defining any functions


import Data.List

uniqueNum :: Eq a => [a] -> Int


uniqueNum = length . nub

• nub is a function in Data.List that takes a list and returns another list with
all duplicates removed

5
Importing Modules
• How to selectively import a few functions?

import Data.List (nub, sort)

• How to import all functions except a few selected ones?

import Data.List hiding (nub)

• Documentation for common libraries

https://hoogle.haskell.org

6
Example: Data.Char
• Given an Int, compute the sum of all its digits

• Data.Char has a function called digitToInt that converts a digit character to


its corresponding integer

import Data.Char (digitToInt)

digitSum :: Int -> Int


digitSum = sum . map digitToInt . show

7
Qualified Imports
• What if you need to import two functions with the same name?

• Use qualified imports and reference the functions by qualified names


import qualified Data.List

uniqueNum :: Eq a => [a] -> Int


uniqueNum = length . Data.List.nub

• How does Haskell know the dot is not the function composition?
• The dot is between a qualified name and a function, without whitespaces

• You can rename the qualified import to make the name shorter
import qualified Data.List as L

8
Example: Data.Map
• The Map (sometimes referred to as Dictionary) data structure can store
associations between unique keys and values

• The implementation is based on size-balanced binary trees

• A map can be constructed from a list of key-value pairs


ghci> import qualified Data.Map as Map
ghci> :t Map.fromList
Map.fromList :: Ord k => [(k, a)] -> Map.Map k a

• Provide handy functions over maps


ghci> let m = Map.fromList [(1, "a"), (2, "b")]
ghci> Map.size m
2

9
Defining Modules
• When defining a module, we only export those functions that act as the
“interface” and hide other functions for internal implementation
Geometry.hs
module Geometry
( sphereArea, cubeArea ) where

sphereArea :: Float -> Float


sphereArea radius = 4 * pi * (radius ^ 2)

cubeArea :: Float -> Float


cubeArea edge = 6 * rectArea edge edge

rectArea :: Float -> Float -> Float


rectArea x y = x * y

• One module in one file; file name same as module name

10
Hierarchical Modules
• Modules can have a hierarchical structure
• Each module can have several submodules, which can also have their
own submodules
Geometry/Sphere.hs Geometry/Cube.hs
module Geometry.Sphere module Geometry.Cube
( area ) where ( area ) where

area :: Float -> Float area :: Float -> Float


area radius = 4 * pi * (radius ^ 2) area edge = 6 * edge * edge

• Module names must agree to the file paths

11

You might also like