导入与导出 #

一、导入基础 #

1.1 基本导入 #

haskell
-- 导入整个模块
import Data.List

-- 使用
sort [3, 1, 2]  -- [1, 2, 3]
nub [1, 2, 1, 3]  -- [1, 2, 3]

1.2 选择性导入 #

haskell
-- 只导入特定函数
import Data.List (sort, nub, group)

-- 使用
sort [3, 1, 2]  -- OK
-- intersperse  -- 错误!未导入

1.3 排除导入 #

haskell
-- 导入除指定函数外的所有内容
import Data.List hiding (head, tail)

-- 使用
sort [3, 1, 2]  -- OK
-- head  -- 错误!被排除

二、限定导入 #

2.1 qualified导入 #

haskell
-- 限定导入
import qualified Data.List as L

-- 使用模块前缀
L.sort [3, 1, 2]  -- [1, 2, 3]
L.nub [1, 2, 1]   -- [1, 2, 3]

-- 不加前缀无法使用
-- sort  -- 错误!

2.2 混合导入 #

haskell
-- 同时导入限定和非限定
import Data.List (sort)
import qualified Data.List as L

-- 使用
sort [3, 1, 2]    -- OK
L.sort [3, 1, 2]  -- OK
L.nub [1, 2, 1]   -- OK

2.3 常用限定导入 #

haskell
import qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.Text as T
import qualified Data.ByteString as B

-- 使用
M.fromList [(1, "one"), (2, "two")]
S.fromList [1, 2, 3, 2, 1]
T.pack "hello"
B.pack [72, 101, 108, 108, 111]

三、导出控制 #

3.1 导出函数 #

haskell
module MyLib (
    -- 导出函数
    func1,
    func2
) where

func1 :: Int -> Int
func1 = (*2)

func2 :: String -> String
func2 = (++ "!")

-- 内部函数
internalFunc :: Int -> Int
internalFunc = (+1)

3.2 导出类型 #

haskell
module MyTypes (
    -- 导出类型和所有构造器
    Person(..),
    
    -- 导出类型但不导出构造器
    Secret,
    
    -- 导出类型和部分构造器
    Result(Success)
) where

-- 完全导出
data Person = Person
    { name :: String
    , age  :: Int
    }

-- 抽象类型
data Secret = Secret String

createSecret :: String -> Secret
createSecret = Secret

-- 部分导出
data Result a
    = Success a
    | Failure String

3.3 导出类型类 #

haskell
module MyClasses (
    -- 导出类型类和所有方法
    MyShow(..),
    
    -- 导出类型类但不导出某些方法
    MyEq(myEq)
) where

class MyShow a where
    myShow :: a -> String
    myShowList :: [a] -> String

class MyEq a where
    myEq :: a -> a -> Bool
    myNe :: a -> a -> Bool

3.4 重导出 #

haskell
module MyPrelude (
    -- 重导出其他模块
    module Data.List,
    module Data.Maybe,
    module Control.Monad
) where

import Data.List
import Data.Maybe
import Control.Monad

四、Prelude #

4.1 隐式导入 #

haskell
-- Prelude自动导入
-- 无需显式import

-- 常用函数
id :: a -> a
const :: a -> b -> a
(.) :: (b -> c) -> (a -> b) -> (a -> c)
flip :: (a -> b -> c) -> b -> a -> c

4.2 隐藏Prelude #

haskell
-- 隐藏Prelude中的某些函数
import Prelude hiding (head, tail)

-- 自定义版本
head :: [a] -> Maybe a
head []    = Nothing
head (x:_) = Just x

4.3 替换Prelude #

haskell
-- 完全不导入Prelude
{-# LANGUAGE NoImplicitPrelude #-}

-- 使用自定义Prelude
import MyPrelude

五、导入组织 #

5.1 标准组织方式 #

haskell
module MyModule (
    -- 导出列表
) where

-- 标准库
import Data.List (sort, nub)
import Data.Maybe (fromMaybe)
import Control.Monad (forM_, when)

-- 第三方库
import qualified Data.Text as T
import qualified Data.Map as M

-- 本地模块
import MyLib.Types
import MyLib.Utils

5.2 导入分组 #

haskell
module MyModule where

-- 类型相关
import Data.Int (Int)
import Data.String (String)

-- 列表相关
import Data.List (sort, filter, map)

-- Maybe相关
import Data.Maybe (Maybe(..), fromMaybe)

-- Monad相关
import Control.Monad (Monad(..), forM_)

六、常见模式 #

6.1 工具模块 #

haskell
-- MyLib/Utils.hs
module MyLib.Utils (
    -- 字符串工具
    trim,
    split,
    
    -- 列表工具
    chunks,
    unique
) where

trim :: String -> String
trim = ...

split :: Char -> String -> [String]
split = ...

chunks :: Int -> [a] -> [[a]]
chunks = ...

unique :: Eq a => [a] -> [a]
unique = ...

6.2 类型模块 #

haskell
-- MyLib/Types.hs
module MyLib.Types (
    -- 核心类型
    Config(..),
    Result(..),
    Error(..),
    
    -- 类型别名
    Name,
    Age
) where

type Name = String
type Age = Int

data Config = Config
    { configHost :: String
    , configPort :: Int
    }

data Result a = Success a | Failure Error

data Error
    = NetworkError String
    | ParseError String
    | UnknownError

6.3 API模块 #

haskell
-- MyLib.hs
module MyLib (
    -- 重导出所有子模块
    module MyLib.Types,
    module MyLib.Core,
    module MyLib.Utils
) where

import MyLib.Types
import MyLib.Core
import MyLib.Utils

七、总结 #

导入与导出要点:

  1. 导入import Module
  2. 选择性导入import Module (func1, func2)
  3. 排除导入import Module hiding (func)
  4. 限定导入import qualified Module as M
  5. 导出函数func
  6. 导出类型Type(..)Type
  7. 重导出module Some.Module

掌握导入与导出后,让我们继续学习常用标准库。

最后更新:2026-03-27