Haskell stack

安裝

brew install haskell-stack

建立專案

1
2
3
4
5
stack new my-project
cd my-project
stack setup
stack build
stack exec my-project-exe

REPL

stack ghci

專案結構

1
2
3
4
5
6
7
8
9
10
11
.
├── LICENSE
├── Setup.hs
├── app
│ └── Main.hs
├── my-project.cabal
├── src
│ └── Lib.hs
├── stack.yaml
└── test
└── Spec.hs

使用別人 Lib

stack install aeson

my-project.cabal
1
2
build-depends:       base >= 4.7 && < 5
,aeson

執行,測試

https://docs.haskellstack.org/en/stable/build_command/
1
2
3
4
stack test is the same as stack build --test
stack bench is the same as stack build --bench
stack haddock is the same as stack build --haddock
stack install is the same as stack build --copy-bins

Stack, Haskell 教學相關

Json Parse (aeson)

ghci
1
2
3
4
5
6
7
8
9
-- ghci -isrc/utils -isrc/subsystem
:set -isrc/utils -isrc/subsystem
:load "blah.hs"
let json = BS.pack "{\"lines\":1,\"date\":\"2012-01-04T01:44:22.964Z\",\"paste-id\":\"1\",\"fork\":null,\"random-id\":\"f1fc1181fb294950ca4df7008\",\"language\":\"Clojure\",\"private\":false,\"url\":\"https://www.refheap.com/paste/1\",\"user\":\"raynes\",\"contents\":\"(begin)\"}"
decode json :: Maybe Paste
-- Just (Paste {getLines = 1, getDate = "2012-01-04T01:44:22.964Z", getID = "1", getLanguage = "Clojure", getPrivate = False, getURL = "https://www.refheap.com/paste/1", getUser = Just "raynes", getBody = "(begin)"})
let (Just x) = decode json :: Maybe Paste
getDate x
-- "2012-01-04T01:44:22.964Z"