创建并安装自己的模块

opt*_*enk 8 haskell

我想创建一个模块,并将其加载到haskell库中.我可以使用haskell-platform的工具与ubuntu 11或Windows 7一起工作.这是我的模块:

module Pkr.Element(..) where

import Char

data Card = Card Int deriving (Eq)

seme :: Card -> Int
seme (Card x) = mod (div x 13) 4

label :: Card -> Int
label (Card x) = mod x 13

instance Ord Card where
(>) x y     |ix == iy       = False
        |ix == 0        = True
        |iy == 0        = False
        | otherwise     = (ix > iy)
        where 
        ix = label x
        iy = label y

instance Show Card where
show :: Card -> String
show card =     strI(label card)    :   strS(seme card) :[] 
where
strI x  |   (x == 0)    = 'A'
    |   (x == 12)   = 'K' 
    |   (x == 11)   = 'Q'
    |   (x == 10)   = 'J'
    |   (x == 9)    = 'T'
    |   otherwise   = chr (49+x)
strS y  |   (y == 0)    = 'h'
    |   (y == 1)    = 'c'
    |   (y == 2)    = 'd'
    |   (y == 3)    = 's'

data Category = Null | HighCard | Copple | TwoCopple | 
        Tris | Straight | Flush | FullHouse |
         Poker | StraightFlush deriving (Show, Eq, Ord)

type Cards = [Card]
data Rank = Rank Category Cards Cards deriving (Eq, Ord, Show)
Run Code Online (Sandbox Code Playgroud)

我在ghci中也有一些"show"的问题,因为我得到了堆栈溢出的异常.

app*_*ive 15

你的粘贴正在弄乱代码; 也许它看起来应该如此:http://hpaste.org/54363(注意hlint底部的建议.)以下步骤是一个简单的开发方法,但我认为会暴露大部分相关因素:

  • 将模块重命名为更合理Poker.Elements,将其另存为Elements.hs

  • 创建一个以名为poker的子目录命名的目录Poker

  • 移动Elements.hspoker/Poker.现在它的层次名称Poker.Elements是有道理的.您的目录结构如下所示:

    -- poker -- Poker -- Elements.hs 
    
    Run Code Online (Sandbox Code Playgroud)
  • poker现在组织得很好.ghci Poker/Elements.hs从那里键入ghci并将知道如何处理Poker.x.y.z更复杂结构中的任何其他模块,比如说这个:

    -- poker -- Poker -- Internal -- Guts.hs (i.e.Poker.Internal.Guts)          
                      |
                      -- Elements.hs  (i.e. Poker.Elements)
    
    Run Code Online (Sandbox Code Playgroud)

但我们的想法是poker使用该cabal工具构建和安装库.没什么比这更简单了.

  • cdpoker,如果你不存在.

  • cabal init.答案都很明显.您正在建立一个与游戏相关的图书馆.

  • 编辑新poker.cabal文件 - cabal init无法分辨您正在使用的软件包.
    事实上,你只是使用PreludeData.Char哪些base,所以扩展Build-depends线看起来如此:

    Build-depends:       base > 2
    
    Run Code Online (Sandbox Code Playgroud)

    结果将如下所示:http://hpaste.org/54364 (如果您缺少任何其他依赖项,则会出现下一个命令.)

  • 您的目录现在具有以下结构:

    -- poker   -- poker.cabal
              |
               -- Poker      -- Elements.hs
    
    Run Code Online (Sandbox Code Playgroud)
  • 因此,您现在拥有一个cabalized,可构建,实际上可以破解的包.输入cabal install,然后cabal clean.该cabal工具管理包的配置,编译,安装和注册.(默认情况下,已编译的库将存放在隐藏目录$HOME/.cabal/lib/poker-0.1或系统的等效目录中.)

  • ghci从系统的任何位置打开; 类型import Poker.Elements.请享用.如果ghc直接调用也没有什么不同- 例如,如果你使用可执行文件ghc --make -O2 PokerCalculator.hs -o pokercalculator,ghc现在将知道如何在Poker.Elements没有进一步指令的情况下查找.

  • 测试你的定义.反映.烦恼.重新考虑.测试更多.看看quickcheck.

  • 修改模块时,重建并重新安装cabal install.这将覆盖旧的安装poker-0.1,因为你仍在调用它; 但在这个最简单的情况下,没有其他包正在构建,所以结果是可取的.(如果您的其他实验库,texas-holdem-0.1- 下一个目录中的那个 - Poker.Elements尽可能地导入,那么也可以重建它以使用关于Elements扑克的更高级的想法.)

  • 如果添加新模块或从新包导入模块,请在相关行中指定这些模块poker.cabal.如果您忘了,cabal install当您尝试重建时,会礼貌地提醒您这样做...

  • 将您的poker包上传到github或patch-tag或darcsden.完善之后,将其上传到hackage.请注意,即使在github或patch-tag上,它也属于'hackaged'宇宙.如果你从Hackage票友包,然后当人们导入模块git clonedarcs get您的资料库,cabal install将获得适合他们的套餐从hackage.haskell.org.