haskell趣学-模块
更新日期:
文章目录
Haskell 中的模块是含有一组相关的函数,类型和类型类的组合.
Haskell 进程的本质便是从主模块中 引用其它模块并调用其中的函数来执行操作
Prelude
模块包含一些基本函数,类型以及类型类,它缺省自动装载
1 2 3 4 | import Data.List -- nub 筛掉重复元素 numUniques :: (Eq a) => [a] -> Int numUniques = length . nub |
在 ghci 中装载模块
1 | ghci> :m Data.List Data.Map Data.Set |
import 各种语法:
1 2 3 4 5 6 7 8 9 10 | -- 仅装载 Data.List 模块 nub 和 sort import Data.List (nub, sort) -- 装载 Data.List 除了 nub import Data.List hiding (nub) -- 使用: Data.Map.filter import qualified Data.Map -- 更加简短: M.filter import qualified Data.Map as M |
翻阅标准库中的模块和函数是提升个人 Haskell 水平的重要途径 Hoogle
Data.List
, Data.Char
, Data.Map
, Data.Set
使用详解:
http://learnyouahaskell-zh-tw.csie.org/zh-cn/modules.html
建立自己的模块
构造一个由计算机几何图形体积和编辑组成的模块 Geometry.hs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | module Geometry ( sphereVolumn , sphereArea , cubeVolume , cubeArea , cuboidArea , cuboidVolume ) where shpereVolume :: Float -> Float sphereVolume = (4.0 / 3.0) * pi * (raius^3) sphereArea :: Float -> Float sphereArea radius = 4 * pi * (radius ^ 2) cubeVolume :: Float -> Float cubeVolume side = cuboidVolume side side side cubeArea :: Float -> Float cubeArea side = cuboidArea side side side cuboidVolume :: Float -> Float -> Float -> Float cuboidVolume a b c = rectangleArea a b * c cuboidArea :: Float -> Float -> Float -> Float cuboidArea a b c = rectangleArea a b * 2 + rectangleArea a c * 2 + rectangleArea c b * 2 rectangleArea :: Float -> Float -> Float rectangleArea a b = a * b |
可以把 Geometry
分成三个子模块
sphere.hs
1 2 3 4 5 6 7 8 9 10 | module Geometry.Sphere ( volume ,area ) where volume :: Float -> Float volume radius = (4.0 / 3.0) * pi * (radius ^ 3) area :: Float -> Float area radius = 4 * pi * (radius ^ 2) |
cuboid.hs
1 2 3 4 5 6 7 | module Geometry.Cuboid ( volume ,area ) where volume :: Float -> Float -> Float -> Float volume a b c = rectangleArea a b * c |
cube.hs
1 2 3 4 5 6 7 8 9 10 11 12 | module Geometry.Cube ( volume ,area ) where import qualified Geometry.Cuboid as Cuboid volume :: Float -> Float volume side = Cuboid.volume side side side area :: Float -> Float area side = Cuboid.area side side side |