4
1

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 ── 02. 基本データ型

Last updated at Posted at 2023-01-02

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

整数

> 255.
255

整数を2進数、8進数、16進数で表記することも可能です。

> 2#0110.
6

> 8#0644.
420

> 16#1F.
31

浮動小数

> 3.14.
3.14

1未満の小数の「0」を省略することはできません。

> 0.5.
0.5

> .5.
* 1:1: syntax error before: '.'

64ビットの倍精度で、指数eに対応しています。

> 1.0e-10.
1.0e-10

アトム

自身の名前がそのまま値になる定数です。全て小文字で表記します。

> foo.
foo

> foo =:= foo.
true

> foo =:= bar.
false

> is_atom(true).
true

> is_atom(false).
true

空白や特殊な文字を含む場合は一重引用符( )で囲みます。

> 'email address'.
'email address'

> '闘魂'.
'闘魂'

> foo =:= 'foo'.
true

Erlangのビルトイン関数も含めたライブラリのモジュールを参照するのにも使用されます。

> crypto:strong_rand_bytes(3).
<<122,169,4>>

真理値

Erlangには型としてのBooleanはありません。true アトムと false アトムでBooleanが表現されています。

> true.
true

> false.
false

> true =:= 'true'.
true

> false =:= 'false'.
true

文字列

文字列は二重引用符( )で囲みます。公式マニュアルによると、Erlangには型としての文字列は存在しません。文字列は符号点のリストの省略形という位置づけだそうです。

> Aisatsu = "hello".
"hello"

> Aisatsu = [$h, $e, $l, $l, $o].
"hello"

> Aisatsu = [104, 101, 108, 108, 111].
"hello"

文字列を操作するための関数を提供するstringモジュールのドキュメントによると、stringモジュールにおける文字列は、unicode:chardata()、つまり、符号点のリスト、UTF-8でエンコードされた符号点を持つバイナリ(UTF-8バイナリ)、またはその組み合わせで表現されます。

"abc".                % 文字列である
<<"abc">>.            % 文字列である
["abc"].              % 文字列である
<<"abc..闘魂"/utf8>>.  % 文字列である
<<"abc..闘魂">>.       % 文字列でない
[<<"abc">>, "..闘魂"]. % 文字列である

バイナリに多バイト文字が含まれる場合はUTF-8と明示しないと実行時にエラーになるようです。

Unicode関連の関数はunicodeモジュールにあります。

バイナリ関連の関数はbinaryモジュールにあります。

listsモジュールも役に立つかもしれません。

Elixirにも挑戦したい

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

4
1
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
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?