从书Beginning Haskell中,我了解到可以从cabal安装文件(chapter2.cabal)构建一个包。源代码可从http://www.apress.com/downloadable/download/sample/sample_id/1516/在Haskell调用由cabal构建的库中的函数
例如,这是第2节示例中Cabal文件的一个示例。
name: chapter2
version: 0.1
cabal-version: >=1.2
build-type: Simple
author: Alejandro Serrano
library
hs-source-dirs: src
build-depends: base >= 4
ghc-options: -Wall
exposed-modules:
Chapter2.Section2.Example,
Chapter2.SimpleFunctions
other-modules:
Chapter2.DataTypes,
Chapter2.DefaultValues
在cabal build
之后,我可以得到动态和静态库的编译。
.
├── Setup.hs
├── chapter2.cabal
├── dist
│ ├── build
│ │ ├── Chapter2
...
│ │ ├── autogen
│ │ │ ├── Paths_chapter2.hs
│ │ │ └── cabal_macros.h
│ │ ├── libHSchapter2-0.1-ghc7.8.3.dylib <-- dynamic lib
│ │ └── libHSchapter2-0.1.a <-- static lib
│ ├── package.conf.inplace
│ └── setup-config
└── src
└── Chapter2
├── DataTypes.hs
├── DefaultValues.hs
├── Section2
│ └── Example.hs
└── SimpleFunctions.hs
然后,我该如何使用其他Haskell代码的库函数(在ghc和ghci中)?例如,src/Chapter2/SimpleFunctions.hs有maxim
函数,我怎样才能调用这个以Haskell库的形式编译的函数?
maxmin list = let h = head list
in if null (tail list)
then (h, h)
else (if h > t_max then h else t_max
, if h < t_min then h else t_min)
where t = maxmin (tail list)
t_max = fst t
t_min = snd t