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
| Module | Key Functions |
|---|---|
Data.Char | toUpper, toLower, isAlpha, isDigit |
Data.List | sort, nub, group, intercalate |
Data.Map | fromList, lookup, insert, member |
Data.Maybe | fromMaybe, isJust, isNothing |
Your Task
- Import
Data.Char(specificallytoUpperandisAlpha) - Import
Data.List(specificallysortandintercalate) - Define
shoutthat converts a string to uppercase usingmap toUpper - Define
onlyLettersthat filters a string to only alphabetic characters usingfilter isAlpha - Print
shout "hello" - Print
onlyLetters "h3ll0 w0rld" - Print
sort [3, 1, 4, 1, 5] - Print
intercalate ", " ["one", "two", "three"]
Haskell loading...
Loading...
Click "Run" to execute your code.