7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Haskellの基本まとめ1

Last updated at Posted at 2013-09-20

Haskellの特徴

  • 純粋関数型プログラミング言語(⇔命令形プログラミング言語)
  • 設定した変数の値は変更出来ない
  • 同じ引数で呼ばれた関数は同じ結果を返す(参照透明性
  • 結果が必要になるまで関数を実行しない(遅延評価
  • 静的型付け言語
  • コンパイラは型を自力で導き出す(型推論
  • エレガントで簡潔なコード

Haskellの開発環境

  • コンパイラ
    • GHC ( The Glasgow Haskell Compiler )
    • Haskell Platform ( GHC + Haskellライブラリ )
  • 任意のテキストエディタ

1. 対話モードでの学習

1-1 基本

  • 対話モードの起動
    • $ ghci
  • 対話モードの終了
    • > :quit or > :q
  • スクリプトファイル(myfunction.hs)の読み込み(※ファイルはカレントディレクトリに)
    • > :l myfunction.hs
  • スクリプトファイルの再読み込み
    • > :l myfunction.hs or > :r

1-2 簡単な計算

Haskellでは、一般的な四則演算の優先順位に従って実行する。

> 1 + 1
2

> 200 - 110
90

> 11 * 11
121

> 5 / 2
2.5

1-3 ブール代数

Haskellの真理値は、"True"と"False"がある。
論理積( && )や論理和( || )、論理否定( not )のための演算子もある。
値の比較には、"==" or "/=" を使う。

> True && False
False

> True && True
True

> False || True
True

> not False
True

> not (True && True)
False

> 5 == 5
True

> 1 == 0
False

> 5 /= 5
False

> "hello" == "hello"
True

1-4 関数の呼び出し

中置関数:2つの引数で関数を挟む

// 中置関数の例
> 2 * 2
4

前置関数:関数名の後にスペースで区切って引数を書く

// 前置関数の例
> succ 8
9

前置関数を中置関数として呼び出す:関数をバッククオート( ` )で括る

// 下記は同じ意味
> div 10 2
> 10 `div` 2
7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?