Erlang基本类型 #
一、类型概述 #
Erlang是一种动态类型语言,类型在运行时确定。主要数据类型包括:
| 类型 | 说明 | 示例 |
|---|---|---|
| integer | 整数 | 42, 16#FF |
| float | 浮点数 | 3.14, 1.0e10 |
| atom | 原子 | ok, error, 'Hello' |
| boolean | 布尔值 | true, false |
| reference | 引用 | make_ref() |
| pid | 进程标识符 | <0.1.0> |
| port | 端口 | #Port<0.1> |
| function | 函数 | fun(X) -> X end |
| list | 列表 | [1, 2, 3] |
| tuple | 元组 | {ok, value} |
| map | 映射 | #{key => value} |
| binary | 二进制 | <<1, 2, 3>> |
二、整数 #
2.1 整数表示 #
erlang
-module(integers).
-export([demo/0]).
demo() ->
Decimal = 42,
Hex = 16#FF,
Octal = 8#77,
Binary = 2#1010,
Base36 = 36#ZZ,
io:format("Decimal: ~p~n", [Decimal]),
io:format("Hex: ~p~n", [Hex]),
io:format("Octal: ~p~n", [Octal]),
io:format("Binary: ~p~n", [Binary]),
io:format("Base36: ~p~n", [Base36]),
ok.
2.2 任意精度 #
Erlang整数是任意精度的:
erlang
1> 123456789012345678901234567890.
123456789012345678901234567890
2> 100 * 100 * 100 * 100 * 100 * 100.
1000000000000
2.3 整数运算 #
erlang
-module(int_arithmetic).
-export([demo/0]).
demo() ->
Sum = 10 + 5,
Diff = 10 - 5,
Product = 10 * 5,
Quotient = 10 div 3,
Remainder = 10 rem 3,
io:format("Sum: ~p~n", [Sum]),
io:format("Diff: ~p~n", [Diff]),
io:format("Product: ~p~n", [Product]),
io:format("Quotient: ~p~n", [Quotient]),
io:format("Remainder: ~p~n", [Remainder]),
ok.
2.4 类型检测 #
erlang
1> is_integer(42).
true
2> is_integer(3.14).
false
3> is_integer("hello").
false
三、浮点数 #
3.1 浮点数表示 #
erlang
-module(floats).
-export([demo/0]).
demo() ->
Pi = 3.14159,
Scientific = 1.0e10,
Small = 1.0e-10,
io:format("Pi: ~p~n", [Pi]),
io:format("Scientific: ~p~n", [Scientific]),
io:format("Small: ~p~n", [Small]),
ok.
3.2 浮点运算 #
erlang
-module(float_arithmetic).
-export([demo/0]).
demo() ->
Sum = 3.14 + 2.86,
Diff = 5.5 - 1.5,
Product = 2.5 * 4.0,
Quotient = 10.0 / 3.0,
io:format("Sum: ~p~n", [Sum]),
io:format("Diff: ~p~n", [Diff]),
io:format("Product: ~p~n", [Product]),
io:format("Quotient: ~p~n", [Quotient]),
ok.
3.3 数学函数 #
erlang
-module(math_funcs).
-export([demo/0]).
demo() ->
Sqrt = math:sqrt(16),
Pow = math:pow(2, 10),
Sin = math:sin(math:pi() / 2),
Cos = math:cos(0),
Log = math:log(2.718281828),
Exp = math:exp(1),
io:format("Sqrt(16): ~p~n", [Sqrt]),
io:format("Pow(2, 10): ~p~n", [Pow]),
io:format("Sin(pi/2): ~p~n", [Sin]),
io:format("Cos(0): ~p~n", [Cos]),
io:format("Log(e): ~p~n", [Log]),
io:format("Exp(1): ~p~n", [Exp]),
ok.
常用数学函数:
| 函数 | 说明 |
|---|---|
math:sqrt/1 |
平方根 |
math:pow/2 |
幂运算 |
math:sin/1 |
正弦 |
math:cos/1 |
余弦 |
math:tan/1 |
正切 |
math:log/1 |
自然对数 |
math:log10/1 |
常用对数 |
math:exp/1 |
e的幂 |
math:pi/0 |
圆周率 |
math:abs/1 |
绝对值 |
3.4 类型转换 #
erlang
1> float(42).
42.0
2> round(3.7).
4
3> trunc(3.7).
3
4> ceil(3.1).
4
5> floor(3.9).
3
3.5 类型检测 #
erlang
1> is_float(3.14).
true
2> is_float(42).
false
3> is_number(42).
true
4> is_number(3.14).
true
四、原子 #
4.1 原子定义 #
原子是字面量常量,以小写字母开头或用单引号包围:
erlang
-module(atoms_demo).
-export([demo/0]).
demo() ->
Simple = ok,
Quoted = 'Hello World',
WithHyphen = 'content-type',
Empty = '',
io:format("Simple: ~p~n", [Simple]),
io:format("Quoted: ~p~n", [Quoted]),
io:format("WithHyphen: ~p~n", [WithHyphen]),
io:format("Empty: ~p~n", [Empty]),
ok.
4.2 原子特点 #
erlang
-module(atom_features).
-export([demo/0]).
demo() ->
true,
false,
ok,
error,
undefined,
io:format("true is atom: ~p~n", [is_atom(true)]),
io:format("false is atom: ~p~n", [is_atom(false)]),
io:format("ok is atom: ~p~n", [is_atom(ok)]),
ok.
4.3 原子比较 #
erlang
1> ok == ok.
true
2> ok == error.
false
3> ok =:= ok.
true
4.4 原子排序 #
原子按字典序排序:
erlang
1> a < b.
true
2> 'a' < 'b'.
true
3> abc < xyz.
true
4.5 原子函数 #
erlang
-module(atom_funcs).
-export([demo/0]).
demo() ->
Atom = hello,
List = atom_to_list(Atom),
Binary = atom_to_binary(Atom, utf8),
io:format("Atom: ~p~n", [Atom]),
io:format("List: ~p~n", [List]),
io:format("Binary: ~p~n", [Binary]),
NewAtom = list_to_atom("world"),
ExistingAtom = list_to_existing_atom("ok"),
io:format("NewAtom: ~p~n", [NewAtom]),
io:format("ExistingAtom: ~p~n", [ExistingAtom]),
ok.
常用原子函数:
| 函数 | 说明 |
|---|---|
atom_to_list/1 |
原子转列表 |
list_to_atom/1 |
列表转原子 |
list_to_existing_atom/1 |
列表转已存在原子 |
atom_to_binary/2 |
原子转二进制 |
binary_to_atom/2 |
二进制转原子 |
4.6 原子使用注意事项 #
erlang
-module(atom_warning).
-export([demo/0]).
demo() ->
ok.
警告:不要动态创建大量原子!原子不会被垃圾回收,过多原子会导致内存耗尽。
五、布尔值 #
5.1 布尔值定义 #
布尔值是特殊的原子 true 和 false:
erlang
-module(booleans).
-export([demo/0]).
demo() ->
True = true,
False = false,
io:format("True: ~p~n", [True]),
io:format("False: ~p~n", [False]),
io:format("is_boolean(true): ~p~n", [is_boolean(true)]),
io:format("is_boolean(ok): ~p~n", [is_boolean(ok)]),
ok.
5.2 逻辑运算 #
erlang
-module(logic_ops).
-export([demo/0]).
demo() ->
And = true and false,
Or = true or false,
Not = not true,
Xor = true xor false,
AndAlso = true andalso false,
OrElse = true orelse false,
io:format("And: ~p~n", [And]),
io:format("Or: ~p~n", [Or]),
io:format("Not: ~p~n", [Not]),
io:format("Xor: ~p~n", [Xor]),
io:format("AndAlso: ~p~n", [AndAlso]),
io:format("OrElse: ~p~n", [OrElse]),
ok.
5.3 比较结果 #
比较运算返回布尔值:
erlang
1> 1 < 2.
true
2> 1 > 2.
false
3> 1 == 1.
true
4> 1 =:= 1.0.
false
六、引用 #
6.1 创建引用 #
erlang
-module(references).
-export([demo/0]).
demo() ->
Ref1 = make_ref(),
Ref2 = make_ref(),
io:format("Ref1: ~p~n", [Ref1]),
io:format("Ref2: ~p~n", [Ref2]),
io:format("Ref1 == Ref2: ~p~n", [Ref1 == Ref2]),
ok.
6.2 引用用途 #
引用常用于:
- 唯一标识符
- 请求-响应匹配
- 消息确认
erlang
-module(ref_usage).
-export([request/2, loop/0]).
request(Pid, Msg) ->
Ref = make_ref(),
Pid ! {request, self(), Ref, Msg},
receive
{response, Ref, Result} -> Result
after 5000 -> timeout
end.
loop() ->
receive
{request, From, Ref, Msg} ->
Result = process(Msg),
From ! {response, Ref, Result},
loop()
end.
process(Msg) -> {processed, Msg}.
七、进程标识符 #
7.1 Pid表示 #
erlang
-module(pids).
-export([demo/0]).
demo() ->
Self = self(),
io:format("Self: ~p~n", [Self]),
io:format("is_pid: ~p~n", [is_pid(Self)]),
Other = spawn(fun() -> ok end),
io:format("Other: ~p~n", [Other]),
ok.
7.2 Pid操作 #
erlang
1> Self = self().
<0.1.0>
2> is_pid(Self).
true
3> is_process_alive(Self).
true
4> Pid = list_to_pid("<0.1.0>").
<0.1.0>
5> pid_to_list(Pid).
"<0.1.0>"
八、端口 #
8.1 端口表示 #
erlang
-module(ports).
-export([demo/0]).
demo() ->
Port = open_port({spawn, "ls"}, []),
io:format("Port: ~p~n", [Port]),
io:format("is_port: ~p~n", [is_port(Port)]),
port_close(Port),
ok.
8.2 端口用途 #
端口用于:
- 与外部程序通信
- 驱动程序接口
九、函数 #
9.1 函数类型 #
erlang
-module(func_types).
-export([demo/0]).
demo() ->
Named = fun math:sqrt/1,
Anonymous = fun(X) -> X * 2 end,
io:format("Named: ~p~n", [Named]),
io:format("Anonymous: ~p~n", [Anonymous]),
io:format("is_function: ~p~n", [is_function(Anonymous)]),
io:format("Apply Named: ~p~n", [Named(16)]),
io:format("Apply Anonymous: ~p~n", [Anonymous(5)]),
ok.
9.2 函数信息 #
erlang
1> F = fun(X) -> X * 2 end.
#Fun<erl_eval.7.126501267>
2> is_function(F).
true
3> erlang:fun_info(F).
[{module,erl_eval},
{name,"-fun/2-fun-0-"},
{arity,1},
{env,[]},
{type,local}]
十、类型检测函数 #
10.1 基本类型检测 #
| 函数 | 说明 |
|---|---|
is_atom/1 |
是否为原子 |
is_binary/1 |
是否为二进制 |
is_bitstring/1 |
是否为位串 |
is_boolean/1 |
是否为布尔值 |
is_float/1 |
是否为浮点数 |
is_function/1 |
是否为函数 |
is_function/2 |
是否为指定参数个数的函数 |
is_integer/1 |
是否为整数 |
is_list/1 |
是否为列表 |
is_map/1 |
是否为映射 |
is_number/1 |
是否为数字 |
is_pid/1 |
是否为进程标识符 |
is_port/1 |
是否为端口 |
is_reference/1 |
是否为引用 |
is_tuple/1 |
是否为元组 |
10.2 使用示例 #
erlang
-module(type_check).
-export([describe/1]).
describe(X) when is_integer(X) -> integer;
describe(X) when is_float(X) -> float;
describe(X) when is_atom(X) -> atom;
describe(X) when is_list(X) -> list;
describe(X) when is_tuple(X) -> tuple;
describe(X) when is_map(X) -> map;
describe(X) when is_binary(X) -> binary;
describe(X) when is_function(X) -> function;
describe(X) when is_pid(X) -> pid;
describe(X) when is_port(X) -> port;
describe(X) when is_reference(X) -> reference;
describe(_) -> unknown.
十一、类型转换 #
11.1 数值转换 #
erlang
-module(type_conversion).
-export([demo/0]).
demo() ->
Int = 42,
Float = float(Int),
Round = round(3.7),
Trunc = trunc(3.7),
Ceil = ceil(3.1),
Floor = floor(3.9),
io:format("Int: ~p~n", [Int]),
io:format("Float: ~p~n", [Float]),
io:format("Round: ~p~n", [Round]),
io:format("Trunc: ~p~n", [Trunc]),
io:format("Ceil: ~p~n", [Ceil]),
io:format("Floor: ~p~n", [Floor]),
ok.
11.2 常用转换函数 #
| 函数 | 说明 |
|---|---|
integer_to_list/1 |
整数转列表 |
list_to_integer/1 |
列表转整数 |
float_to_list/1 |
浮点数转列表 |
list_to_float/1 |
列表转浮点数 |
atom_to_list/1 |
原子转列表 |
list_to_atom/1 |
列表转原子 |
binary_to_list/1 |
二进制转列表 |
list_to_binary/1 |
列表转二进制 |
tuple_to_list/1 |
元组转列表 |
list_to_tuple/1 |
列表转元组 |
十二、总结 #
本章学习了:
- 整数类型及其运算
- 浮点数类型及其运算
- 原子类型及其使用
- 布尔值和逻辑运算
- 引用、Pid、端口
- 函数类型
- 类型检测函数
- 类型转换函数
准备好学习更多数据类型了吗?让我们进入下一章。
最后更新:2026-03-27