10

Since things change so fast, I've posted this question so hopefully the community-agreed way to start a Haskell project can be clarified. Imagine I have two separate projects:

  • Project #1: Square, the library that squares numbers. No deps.

    -- Square.hs
    
    module Square where
    
    square :: Num a => a -> a
    square x = x * x
    
  • Project #2: Hypotenuse, the library and executable that finds the longest side of a right-angled triangle. Depends on #1:

    -- Hypotenuse.hs
    
    module Hypotenuse where
    
    import Square
    
    hypotenuse :: Floating a => a -> a -> a
    hypotenuse x y = sqrt $ square x + square y
    

    ,

    -- Main.hs
    
    import System.Environment
    import Hypotenuse
    
    main = do
        [x,y] <- fmap (map read) getArgs
        print $ hypotenuse x y
    

Starting with a computer with GHC 7.10.2, Stack and Cabal installed, and a single directory, ~/OrganizeMe, containing ~/OrganizeMe/Square.hs, ~/OrganizeMe/Hypotenuse.hs and ~/OrganizeMe/Main.hs, as presented above - what is a complete set of unix commands an experienced Haskeller would use to architect those projects? That includes:

  1. Organizing the directory tree of those projects;

  2. configuring Stack/Cabal/etc (and git, optionally);

  3. building/installing them locally;

  4. publishing to Hackage/Stackage.

4

1 回答 1

4

This is not a complete answer, it does not start with your OrganizeMe directories (there are some errors in your code) and it does not include publishing to Hackage/Stackage. I start with a directory stackenv to contain both packages but you could do this quite differently of course.

mkdir stackenv && cd stackenv/
mkdir square && cd square
vim Square.hs # The file you describe without the x in the type of square
cabal init # Go through the questions and choose "library"
stack init
cd ../ && mkdir hypotenuse && cd hypotenuse/
vim Hypotenuse.hs # The file you describe
vim Main.hs # The file you describe but importing Hypotenuse
cabal init # Go through the questions... "executable" this time
stack init
vim hypotenuse.cabal # add "square" or chosen package name to build-depends
vim stack.yaml # add "- '../square/'" below packages
stack install
hypotenuse 3 4 # works because ~/.local/bin is in my PATH

Hope this helps.

于 2015-10-02T15:40:03.923 回答