I want to create a module, and load it in the haskell library. I can work with ubuntu 11, or windows 7, using the tools of the haskell-platform. This is my module:
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)
I have also some problem with “show” in the ghci, because I get an exception of stack overflow.
Your paste is messing with the code; maybe it should look so: http://hpaste.org/54363 (note the advice from
hlintat the bottom.) The following steps are for a simple method of development, but will I think expose most of the relevant factors:Rename the module to the more sensible
Poker.Elements, save it asElements.hsMake a directory named
pokerwith a subdirectory namedPokerMove
Elements.hsintopoker/Poker. Now its hierarchical name,Poker.Elementsmakes sense. Your directory structure looks so:pokeris now organized properly. Typeghci Poker/Elements.hsfrom there andghciwill know how to deal with any otherPoker.x.y.zmodules in a more complicated structure like say this one:But our idea was to build and install the
pokerlibrary with thecabaltool. Nothing simpler.cdintopokerif you are not there.Run
cabal init. The answers will all be obvious. You are making a Library, pertaining to Games.Edit your new
poker.cabalfile —cabal initcan’t tell what packages you are using.In fact, you are only using
PreludeandData.Charwhich are inbase, so extend theBuild-dependsline to look thus:The result will look like this: http://hpaste.org/54364 (If you are missing any other dependencies, this will emerge with the next command.)
Your directory now has this structure:
You thus now have a cabalized, buildable, indeed hackage-able package. Type
cabal install, thencabal clean. Thecabaltool has managed configuration, compilation, installation and registration of the package. (By default the compiled library will be deposited in the hidden directory$HOME/.cabal/lib/poker-0.1or the equivalent for your system.)Open
ghcifrom anywhere on your system; typeimport Poker.Elements. Enjoy. It is no different if you callghcdirectly — for example, if you make an executable withghc --make -O2 PokerCalculator.hs -o pokercalculator, ghc will now know how to findPoker.Elementswithout further instruction.Test your definitions. Reflect. Fret. Reconsider. Test more. Look into
quickcheck.When you revise your module, rebuild and reinstall with
cabal install. This will over-write the old installation ofpoker-0.1, as you are still calling it; but in this, simplest, case, no other packages are being built against it so the result is desirable. (If your other experimental library,texas-holdem-0.1— the one in the next directory –importsPoker.Elements, as is likely, then rebuild it too to use your more advanced ideas about theElementsof poker.)If you add new modules, or import modules from new packages, specify these in the relevant lines of
poker.cabal. If you forget,cabal installwill politely remind you to do this when you try to rebuild …Upload your
pokerpackage to github or patch-tag or darcsden. When you have perfected it, upload it to hackage. Note that even on github or patch-tag it belongs to the ‘hackaged’ universe. If you import modules from fancier packages on Hackage, then when peoplegit cloneordarcs getyour repository,cabal installwill get the right packages for them from hackage.haskell.org.