7
3

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 1 year has passed since last update.

草莽Erlang ── 01. 入門

Last updated at Posted at 2023-01-01

口で言うより行うことがErlang習得への近道と信じています。

Erlangとは

  • 関数型プログラミング言語
  • 数学者のアグナー・アーランにちなんで命名
  • 障害に強い
  • 並行処理
  • 分散コンピューティング

Erlangのインストール

ErlangだけではなくElixir言語も一緒にインストールしておくのがオススメです。

各OS向けのインストール方法は@torifukukaiouさんの闘魂Elixirで探すことができます。

Erlangがインストールされたかは erl コマンドの有無により確認できます。以下のコマンドを打つとerl コマンドへのパスが表示されます。Erlangのバージョンを表示するコマンドは特にないようです。

$ type -a erl

対話モード

ErlangにはErlang shellという対話シェルが付属しており、入力したErlangの式をその場で評価することができるようになっています。

対話モードを開始するには、 erl を起動します。

$ erl
Erlang/OTP 25 [erts-13.1.3] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit] [dtrace]

Eshell V13.1.3  (abort with ^G)
1> 

Erlangの式は英語のように最後に.を打ちます。

> 1 + 1.
2

> 2 = 1 + 1.
2

左辺と右辺は一致している必要があります。

> 99 = 1 + 1.
** exception error: no match of right hand side value 2

Erlangの変数は一文字めが大文字である必要があります。

> X = 3.
3

> X + 1.
4

> y + 1.
** exception error: an error occurred when evaluating an arithmetic expression
     in operator  +/2
        called as y + 1

便宜上「変数」として説明されることが多いですが、一度束縛されれば、値を変更することはできません。変更不可能なので「変数」と呼ぶのは変な気がします。

> Z = 19790101.
19790101

> Z = 19790101.
19790101

> Z = 20230101.
** exception error: no match of right hand side value 20230101

Erlangシェルにはいくつか特殊なヘルパー関数があります。

help/0でヘルパー関数のリストを表示できます。

b/0で現在束縛されている変数を確認できます。

f/0で現在束縛されている変数を全て解放できます。

> X = 1.
1

> b().
X = 1
ok

> f().
ok

> X = 2.
2

Elixirにも挑戦したい

闘魂ElixirシリーズとElixir Schoolがオススメです。

7
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?