haskell使用技巧

.原来GHCI是可以编程的, 真方便

$cat ~/.ghci
:def hoogle \str -> return $ ":! hoogle --count=15 \"" ++ str ++ "\""
:cd /media/G/www/qachina/db/doc/money
:load Money.hs

这里定义了函数hoogle, 当进入ghci后, 就可以用它查寻hoogle了(当然需先装hoogle:

cabal install alex happy && cabal install hoogle


$ghci
*Money> : hoogle (a -> Bool) -> [a] -> ([a], [a])
Prelude break :: (a -> Bool) -> [a] -> ([a], [a])
Prelude span :: (a -> Bool) -> [a] -> ([a], [a])
Data.List break :: (a -> Bool) -> [a] -> ([a], [a])
Data.List partition :: (a -> Bool) -> [a] -> ([a], [a])
Data.List span :: (a -> Bool) -> [a] -> ([a], [a])

看看有无你需要的结果。


 .profiling:


ghc -prof -auto-all -O2 --make Main

  Main.exe +RTS -p --hc -s main.summary


  .限制GHCI的内存使用


   $ghci +RTS -M 100M -c 30 --限制堆大小为100MB,且堆使用增长到30%时压缩堆


  .模块名可以有层次关系的(与文件系统的目录对应)

比如如下文件目录结构:

src/Web
    Page.hs Action.hs
src/Data
Data/Binary
Data/Binary/Defer
            Defer.hs Map.hs Vector.hs Array.hs

Page.hs中就可这样声明模块
module Web.Page

Map.hs中就可这样声明模块
module Data.Binary.Defer.Map

  Defer.hs用于简化模块引用, 使用者只需引入  Data.Binary.Defer就可能使用
 
Data.Binary.Defer.Monad及   Data.Binary.Defer.Class

module Data.Binary.Defer(
    module Data.Binary.Defer.Monad,
    module Data.Binary.Defer.Class
) where
import Data.Binary.Defer.Monad
import Data.Binary.Defer.Class

  . 缩小GHC编译文件大小

$ ghc - o hello hello . hs
$ strip ---strip-unneeded --remove-section=.comment -o hello-small hello
$ du hello hello - small
700 hello
476 hello - small

Add the -dymamic flag for a dynamically linked RTS:

$ ghc - dynamic - o hello hello . hs
$ strip  -p --strip-unneeded --remove-section=.comment -o hello-small hello
$ du hello hello - small

24  hello

16  hello-small


. 堆profiling


堆有问题时运行heap profiler:
$ ghc -O2 --make A.hs -prof -auto-all -rtsopts -fforce-recomp
[1 of 1] Compiling Main ( A.hs, A.o )
Linking A.exe ...

Which when run:
$ ./A.exe +RTS -M1G -hy

Produces an A.hp output file:
$ hp2ps -c A.hp


. 用cabal构建项目

cabal configure [--enable-tests]  --"enable-tests"后,可运行`cabal test`进行测试

cabal build

cabal install [--disable-library-profiling] [--enable-shared] [--enable-executable-dynamic] [--ghc-option=-dynamic]


开发时减少资源占用:

cabal build --ghc-options="-O0 -c" && \
cabal build --ghc-options="-O0 -optl -O-optl --reduce-memory-overheads -optl --hash-size=31"

-optl将参数传递给Linker(ld)。


$ cabal install --enable-split-objs   --减少文件大小

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值