doc: update erlang.md #673

This commit is contained in:
jaywcjlove 2024-06-25 13:01:26 +08:00
parent 0be71ca830
commit 942f2c9563

View File

@ -13,6 +13,7 @@ erl
```
### 编译代码
<!--rehype:wrap-class=row-span-2-->
```shell
# 在 Erlang Shell 中编译
@ -22,6 +23,7 @@ erlc module.erl
```
### 运行代码
<!--rehype:wrap-class=row-span-2-->
```shell
# 在 Erlang Shell 中运行
@ -60,6 +62,148 @@ function_name(Args) ->
```erlang
% 单行注释
% 这是一个注释
```
### 变量
```erlang
VarName = Value. % 变量名必须以大写字母开头
Age = 25.
Name = "Alice".
```
数据类型
---
### 原子
```erlang
atom. % 例子atom, 'Atom with spaces'
```
### 数字
```erlang
123. % 整数
3.14. % 浮点数
```
### 布尔值
```erlang
true.
false.
```
### 字符串
```erlang
"Hello, World!".
```
### 元组
```erlang
{ok, "Success"}.
```
### 列表
```erlang
[1, 2, 3].
[H|T] = [1, 2, 3]. % H = 1, T = [2, 3]
```
### 字典 (Map)
```erlang
#{key1 => value1, key2 => value2}.
```
控制结构
---
### 条件语句
```erlang
if
Condition1 -> Expression1;
Condition2 -> Expression2;
true -> DefaultExpression
end.
```
### case 表达式
```erlang
case Expression of
Pattern1 -> Expression1;
Pattern2 -> Expression2;
_ -> DefaultExpression
end.
```
### 函数定义
```erlang
% 无参函数
my_function() ->
ok.
% 有参函数
add(A, B) ->
A + B.
```
列表操作
---
### 列表生成
```erlang
% 生成 1 到 10 的列表
[ X || X <- lists:seq(1, 10)].
% 生成 1 到 10 中的偶数
[ X || X <- lists:seq(1, 10), X rem 2 == 0].
```
并发
---
### 启动进程
```erlang
spawn(Module, Function, Args).
% 示例
Pid = spawn(fun() -> io:format("Hello from process~n") end).
```
### 发送消息
```erlang
Pid ! Message.
% 示例
Pid ! {hello, self()}.
```
### 接收消息
```erlang
receive
Pattern1 -> Expression1;
Pattern2 -> Expression2;
after Timeout -> TimeoutExpression
end.
```
### 模式匹配
```erlang
{ok, Value} = {ok, 42}.
```
常用内置函数 (BIFs)
@ -98,6 +242,51 @@ file:write_file(Filename, Data).
file:delete(Filename).
```
### 列表操作
```erlang
lists:map(fun(X) -> X * 2 end, [1, 2, 3]).
lists:filter(fun(X) -> X rem 2 == 0 end, [1, 2, 3, 4]).
```
### 字符串操作
```erlang
string:len("Hello").
string:upper("hello").
```
### 文件操作
```erlang
{ok, File} = file:open("test.txt", [write]).
file:write(File, "Hello, file!").
file:close(File).
```
### 示例:简单的服务器
```erlang
-module(server).
-export([start/0, loop/0]).
start() ->
spawn(fun loop/0).
loop() ->
receive
{echo, Msg} ->
io:format("Echo: ~p~n", [Msg]),
loop();
stop ->
io:format("Server stopping~n"),
ok;
_ ->
io:format("Unknown message~n"),
loop()
end.
```
并发编程
---
@ -156,6 +345,16 @@ end.
- `error`
- `exit`
### 错误处理
```erlang
try Expression of
Pattern -> Result
catch
Type:Reason -> ErrorHandlingExpression
end.
```
分布式编程
---