1. Modules Packages Cabal

Created Saturday 23 August 2014

General Introduction


Importing Modules

import Data.Set

Import Qualified

import qualified Data.Set
import qualified Data.Sequence
GHCi> :t Data.Set.empty
Result: Data.Set.empty :: Data.Set.Set a

Import with Renaming

import Data.Sequence as Seq
GHCi> :t Seq.empty
Result: Seq.empty :: Seq a
GHCi> :t empty
Result: empty :: Seq a

Force Qualified with Renaming

import qualified Data.Sequence as Seq

Explicit Import Lists

import Data.Set (empty, size)

Data Types in Explicit Import Lists

import Data.Maybe (Maybe)
import Data.Maybe (Maybe (Just, Nothing))
import Data.Maybe (Maybe (..))

Type Classes in Explicit Import Lists

import Control.Monad (Monad)
import Control.Monad (Monad, return)

Returning Type Class Instances

import Data.Set ()

Import Hiding

import Data.Set hiding (empty, size)
import Prelude hiding (map)

Defining Modules

module MyModule where
module Foo.Bar.Baz where

Export Lists

module Foo.Bar.Baz
, MyType
, MyType2(Constructor1)) where

Exporting Parts of Another Module

module Foo.Bar.Baz
(fromMyModule) where
import MyModule (fromMyModule)
module Foo.Bar.Baz
(module MyModule) where
import MyModule


Installing Packages

Creating Cabal Packages

Demo: Creating a Simple Web App with Cabal

{-# LANGUAGE OverloadedStrings #-} --Using a language extension (conv. for using alt. string data str.)
module Greeting where

import Data.Monoid (mconcat)
import Data.Text.Lazy (Text)

data Language = English | Espanol
deriving (Read)

greeting :: Language -> Text
greeting English = "Hello"
greeting Espanol = "Hola"

greet :: Text -> Language -> Text
greet name language =
mconcat [greeting language, " ", name]

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Web.Scotty
import Data.Monoid (mconcat)
import Greeting

main = scotty 3000 $ do
get "/:language/:name" $ do
name <- param "name"
languageStr <- param "language"
let language = read languageStr
html $ mconcat [ "<h1>", greet name language, "</h1>" ]

Cabal Sandbox