fugafuga.write

日々のログ

すごいH本入門 ―イントロダクション―

Elixir を触っていると関数型プログラミングに興味が湧いてきたので、すごいH本を読んでいこうと思う。 この本を読むことで関数型プログラミングの基本的な考え方を得たい。

そんでもって、めちゃくちゃエロいらしい。

今日は1日目。

イントロダクション

  • Haskell は純粋関数型プログラミング言語である
    • 何をするか、ではなく何であるかをコンピューターに伝える
  • immutable である
  • 関数は副作用を持たない
    • '参照透明性を持つ' と表現される
    • インプットが同じなら必ずアウトプットも同じとなる
  • 遅延評価
    • 結果が必要になるまで関数を実行しない
  • 静的型付け言語である
  • 型推論を持つ
    • 型を明示しなくてもコンパイラがコードから型を推測する
  • エレガントである
    • 短いコード
    • すごい博士たちによってHaskellは作られている

Haskell の開発環境

必要なものは、

  • エディタ
  • Haskell コンパイラ

のみ。

Haskell のコンパイラは GHC が最も広く使用されている。

と、本には書いてあるがググると stack なるものがあるのでそれを導入する。

Home - The Haskell Tool Stack

  • Installing GHC automatically, in an isolated location.
  • Installing packages needed for your project.
  • Building your project.
  • Testing your project.
  • Benchmarking your project.

とあるようにGHCも入れてくれるし、他にも色々便利な機能があるみたい。

とりあえず、stack をインストールする。

curl -sSL https://get.haskellstack.org/ | sh

プロジェクトを作成する

> stack new my-project
Downloading template "new-template" to create project "my-project" in my-project/ ...

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/takuya/.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 my-project 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:
- my-project/my-project.cabal

Selecting the best among 11 snapshots...

Downloaded lts-9.14 build plan.
Selected mirror https://s3.amazonaws.com/hackage.fpcomplete.com/
Downloading root
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 list downloaded
Populated index cache.
* Matches lts-9.14

Selected resolver: lts-9.14
Initialising configuration using resolver: lts-9.14
Total number of user packages considered: 1
Writing configuration to file: my-project/stack.yaml
All done.

コンパイラをダウンロードする

> stack setup
Preparing to install GHC to an isolated location.
This will not interfere with any system-level installation.
Downloaded ghc-8.0.2.
Installed GHC.
stack will use a sandboxed GHC it installed
For more information on paths, see 'stack path' and 'stack exec env'
To use this GHC and packages outside of a project, consider using:
stack ghc, stack ghci, stack runghc, or stack exec

プロジェクトをビルドする

> stack build
...
...
Registering my-project-0.1.0.0...

プログラムを実行する

> stack exec my-project-exe
someFunc

REPL を起動

> stack ghci
...
...
*Main Lib>
*Main Lib> 1 + 1
2

スクリプトを書いてみる

src配下に baby.hs を作成し、以下の内容を入力

doubleMe x = x + x

関数名 {引数} = {関数本体} となっている模様。

REPL 起動し、baby.hs をロードする

> stack ghci
...
...
*Main Lib> :l baby.hs
[1 of 1] Compiling Main             ( baby.hs, interpreted )
Ok, modules loaded: Main.

実行してみる

*Main> doubleMe 8
16

他の関数を baby.hs に加えてみる

doubleUs x y = x * 2 + y * 2

2個引数を取り、それらを2倍して足した値を返す。

REPL で再度 baby.hs をロードして実行してみる

*Main> doubleUs 2 4
12

今日はとりあえずここまで。

すごいHaskellたのしく学ぼう!

すごいHaskellたのしく学ぼう!

  • 作者: MiranLipovaca
  • 出版社/メーカー: オーム社
  • 発売日: 2017/07/14
  • メディア: Kindle版
  • 購入: 4人 クリック: 9回
  • この商品を含むブログを見る