Module structure

A module is simply a bunch of related functions, types and type classes. This makes a program a collection of modules. This helps organize your code and makes reusing some of the code easier.

Module header

Module declaration

This how we declare a new module and specify which of the functions or types are exported.

  1. module Hello (greet, farewell) where
  2. {- the module name can be a word or words separated by '.';
  3. in this case it is just "Hello" -}
  4. greet :: String -> String
  5. greet n = "Hello " ++ n
  6. farewell :: String -> String
  7. farewell n = "Bye " ++ n

Module import

The syntax for import in Hamler is import <module name>. This has to be done before defining any functions. One module can import as many as modules you wish, but there could be ambiguity when there are two things with the same name.

  1. import Data.List --Modules are imported using their full names
  2. import Data.Maybe (isJust, isNothing) -- We can choose which functions to import
  3. import Data.Funtion as F
  4. -- We can deal with ambiguity by adding an alias. This means we need to add "F." before every function that is exposed from Data.Function to specify that it is from this module
  5. import Prelude hiding (fst) -- The Prelude is imported by default. By hiding `fst`, we can define our own version.