Haskell に入門するために開発環境を整える
『すごい H 本』こと『すごい Haskell たのしく学ぼう!』をざざっと読んだので、実際に手を動かしてみたい。ということで、開発環境を整える。
Stack
Stack というツールで GHC (コンパイラ)のインストールやコードのビルドができるらしい。
- Home - The Haskell Tool Stack
- 本気で Haskell したい人向けの Stack チュートリアル - Qiita
- Stackでやる最速Haskell Hello world! (GHCのインストール付き!) - Qiita
まず Homebrew でインストール。
$ brew install stack
新規プロジェクト hello-world
を作る。
$ stack new hello-world Downloading template "new-template" to create project "hello-world" in hello-world/ ... The following parameters were needed by the template but not provided: author-name You can provide them in /Users/mimikakimemo/.stack/config.yaml, like this: templates: params: author-name: value Or you can pass each one as parameters like this: stack new hello-world new-template -p "author-name:value" The following parameters were needed by the template but not provided: author-email, author-name, category, copyright, github-username You can provide them in /Users/mimikakimemo/.stack/config.yaml, like this: templates: params: author-email: value author-name: value category: value copyright: value github-username: value Or you can pass each one as parameters like this: stack new hello-world new-template -p "author-email:value" -p "author-name:value" -p "category:value" -p "copyright:value" -p "github-username:value" Looking for .cabal or package.yaml files to use to init the project. Using cabal packages: - hello-world/ Selecting the best among 15 snapshots... Selected mirror https://s3.amazonaws.com/hackage.fpcomplete.com/ Downloading timestamp Downloading snapshot Downloading mirrors Cannot update index (no local copy) Downloading index Updated package index downloaded Update complete Populated index cache. * Matches lts-13.5 Selected resolver: lts-13.5 Initialising configuration using resolver: lts-13.5 Total number of user packages considered: 1 Writing configuration to file: hello-world/stack.yaml All done.
途中の Downloading index
でやたらと時間がかかってフリーズしたかと思ったが、10分ほどで完了した。
$ cd hello-world $ tree -a . ├── .gitignore ├── ChangeLog.md ├── LICENSE ├── README.md ├── Setup.hs ├── app/ │ └── Main.hs ├── hello-world.cabal ├── package.yaml ├── src/ │ └── Lib.hs ├── stack.yaml └── test/ └── Spec.hs
stack build
で GHC のインストールもしてくれる。
$ stack build Preparing to install GHC to an isolated location. This will not interfere with any system-level installation. Downloaded ghc-8.6.3. Installed GHC. [1 of 2] Compiling Main ( /Users/mimikakimemo/.stack/setup-exe-src/setup-mPHDZzAJ.hs, /Users/mimikakimemo/.stack/setup-exe-src/setup-mPHDZzAJ.o ) [2 of 2] Compiling StackSetupShim ( /Users/mimikakimemo/.stack/setup-exe-src/setup-shim-mPHDZzAJ.hs, /Users/mimikakimemo/.stack/setup-exe-src/setup-shim-mPHDZzAJ.o ) Linking /Users/mimikakimemo/.stack/setup-exe-cache/x86_64-osx/tmp-Cabal-simple_mPHDZzAJ_2.4.0.1_ghc-8.6.3 ... Building all executables for `hello-world' once. After a successful build of all of them, only specified executables will be rebuilt. hello-world-0.1.0.0: configure (lib + exe) Configuring hello-world-0.1.0.0... hello-world-0.1.0.0: build (lib + exe) Preprocessing library for hello-world-0.1.0.0.. Building library for hello-world-0.1.0.0.. [1 of 2] Compiling Lib ( src/Lib.hs, .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/Lib.o ) [2 of 2] Compiling Paths_hello_world ( .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/autogen/Paths_hello_world.hs, .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/Paths_hello_world.o ) Preprocessing executable 'hello-world-exe' for hello-world-0.1.0.0.. Building executable 'hello-world-exe' for hello-world-0.1.0.0.. [1 of 2] Compiling Main ( app/Main.hs, .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/hello-world-exe/hello-world-exe-tmp/Main.o ) [2 of 2] Compiling Paths_hello_world ( .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/hello-world-exe/autogen/Paths_hello_world.hs, .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/hello-world-exe/hello-world-exe-tmp/Paths_hello_world.o ) Linking .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/hello-world-exe/hello-world-exe ... hello-world-0.1.0.0: copy/register Installing library in /Users/mimikakimemo/repos/hello-world/.stack-work/install/x86_64-osx/lts-13.5/8.6.3/lib/x86_64-osx-ghc-8.6.3/hello-world-0.1.0.0-NpaWwN61fJ91LUORGYsB Installing executable hello-world-exe in /Users/mimikakimemo/repos/hello-world/.stack-work/install/x86_64-osx/lts-13.5/8.6.3/bin Registering library for hello-world-0.1.0.0..
stack exec <プロジェクト名>-exe
で実行できるらしい。
$ stack exec hello-world-exe someFunc
用意されている app/Main.hs
の内容。
module Main where import Lib main :: IO () main = someFunc
インポートされている src/Lib.hs
はこうなっている。
module Lib ( someFunc ) where someFunc :: IO () someFunc = putStrLn "someFunc"
よって、最終行を someFunc = putStrLn "Hello, world!"
に書き換えてビルド・実行すれば、
$ stack build hello-world-0.1.0.0: unregistering (local file changes: src/Lib.hs) hello-world-0.1.0.0: build (lib + exe) Preprocessing library for hello-world-0.1.0.0.. Building library for hello-world-0.1.0.0.. [2 of 2] Compiling Lib ( src/Lib.hs, .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/Lib.o ) Preprocessing executable 'hello-world-exe' for hello-world-0.1.0.0.. Building executable 'hello-world-exe' for hello-world-0.1.0.0.. [1 of 2] Compiling Main ( app/Main.hs, .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/hello-world-exe/hello-world-exe-tmp/Main.o ) [Lib changed] Linking .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/hello-world-exe/hello-world-exe ... hello-world-0.1.0.0: copy/register Installing library in /Users/mimikakimemo/repos/hello-world/.stack-work/install/x86_64-osx/lts-13.5/8.6.3/lib/x86_64-osx-ghc-8.6.3/hello-world-0.1.0.0-NpaWwN61fJ91LUORGYsB Installing executable hello-world-exe in /Users/mimikakimemo/repos/hello-world/.stack-work/install/x86_64-osx/lts-13.5/8.6.3/bin Registering library for hello-world-0.1.0.0.. $ stack exec hello-world-exe Hello, world!
Hello, world!
と表示される。
その他
- 作者: Miran Lipovača,田中英行,村主崇行
- 出版社/メーカー: オーム社
- 発売日: 2012/05/23
- メディア: 単行本(ソフトカバー)
- 購入: 25人 クリック: 580回
- この商品を含むブログ (73件) を見る