Haskell Programming Setup
— Create Program
mkdir haq
cd haq/
cat > Haq.hs
import System.Environment
main :: IO()
main = getArgs >>= print . haqify . head
haqify s = “Haq! ” ++ s
— add program to git
git init
git add Haq.hs
git commit -am “my first haskell git commit”
cabal init
git add haq.cabal Setup.hs LICENSE
— build and run it in sandbox
cabal sandbox init
cabal install -j
/Users/rahul/src/haq/.cabal-sandbox/bin/haq ME
sudo cp .cabal-sandbox/bin/haq /usr/local/bin/haq
— create API documentation
cabal configure
cabal haddock
cabal haddock –hyperlink-source –html-location=’http://hackage.haskell.org/package/haq/docs’ –contents-location=’http://hackage.haskell.org/package/haq’
w3m -dump dist/doc/html/haq/Main.html
— check code styling
cabal install hlint
hlint .
— add following to your cabal file (equivalent of build file)
test-suite tests ghc-options: -Wall default-extensions: OverloadedStrings type: exitcode-stdio-1.0 main-is: HSpecTests.hs build-depends: base, haq, hspec
cabal install –enable-tests
cat > HSpecTests.hs module Main where import Haq import Test.Hspec main :: IO () main = hspec $ do describe "Validate haqify function" $ do it "haqify is supposed to prefix Haq! to things" $ do haqify "me" `shouldBe` "Haq! me"
cabal test
— Using Quick Test
cat > QuickTests.hs import Data.Char import Data.List import Haq import Test.QuickCheck import Text.Printf main = mapM_ (\(s,a) -> printf "%-25s: " s >> a) tests -- reversing twice a finite list, is the same as identity prop_reversereverse s = (reverse . reverse) s == id s where _ = s :: [Int] -- Dropping the "Haq! " string is the same as identity prop_haq s = drop (length "Haq! ") (haqify s) == id s where haqify s = "Haq! " ++ s tests = [("reverse.reverse/id", quickCheck prop_reversereverse) ,("drop.haq/id", quickCheck prop_haq)]
To run the test:
$ runhaskell Tests.hs
–Tab stable version of code
git tag 0.0
Reference :
https://www.haskell.org/haskellwiki/How_to_write_a_Haskell_program