May 13, 2021 Erlang
If a programming language can only run code through shells, the language is of little use, and Erlang can also run programs through scripts. H ere's a short section of the Erlang program. E nter it into the file tut.erl using the appropriate text editor. T he file name must be tut.erl and cannot be modified arbitrarily, and it needs to be placed in the directory where you started the erl command. I f it happens that your editor has Erlang mode, the editor will help you organize and format your code gracefully (refer to Emacs' Erlang mode), but even if you don't have one you can manage your own code well. Number, ShoeSize, and Age are all variables.
- -module(geometry). %模块声明,模块名必须与文件名相同。
- -export([area/1]). %导出声明,声明可以外部使用的函数
- area({rectangle, Width, Height}) -> Width*Height; %子句1
- area({square, Side}) -> Side * Side.%子句2
This function area has multiple clauses, which are used between clauses; S
eparate.
- 17> c("ErlangGame/geometry.erl").
- ErlangGame/geometry.erl:1: Warning: Non-UTF-8 character(s) detected, but no encoding declared. Encode the file in UTF-8 or add "%% coding: latin-1" at the beginning of the file. Retrying with latin-1 encoding.
- {ok,geometry}
- c("e:/mywokespace/ErlangGame/geometry.erl").%使用绝对路径
- c("ErlangGame/geometry.erl").%使用相对路径,这个时候我所在的目录是e:/mywokespace/
- c(geometry).%使用相对路径、去掉双引号。因为没有.号,可以使用原子。
The compiled output has a warning:
ErlangGame/geometry.erl:1: Warning: Non-UTF-8 character(s) detected, but no encoding declared. Encode the file in UTF-8 or add "%% coding: latin-1" at the beginning of the file. Retrying with latin-1 encoding.
This is because I wrote a note, which is a Chinese character, using UTF-8. I
f you remove it, you will:
{ok,geometry}
That's the only one.
After compilation, the calling module does not have to add this path.
- 1> Double = fun(x)->2*x end.
- #Fun<erl_eval.6.52032458>
- 2> Double(2).
- ** exception error: no function clause matching
- erl_eval:'-inside-an-interpreted-fun-'(2)
The function definition was successful, but it was error-positive for how it was called.
After trying for a long time, I suddenly found that x was small. I
n Erlang, x is equivalent to 'x' of C. Y
ou can't do variables.
Variables all start in capitals.
- 3> Three = fun(X)-> 3 * X end.
- #Fun<erl_eval.6.52032458>
- 4> Three(2).
- 6
Ok. I
t worked.
- 5> L = [1,2,3,4].
- [1,2,3,4]
- 6> lists:map(Three, L).
- [3,6,9,12]
The module of the standard library is called here. T
he standard library is compiled and can be used directly.
Just pass the function name in.
Lists:map is equivalent to a for loop
- 8> lists:filter(fun(X)->(X rem 2)=:=0 end,[1,2,3,4,5,6,7,8]).
- [2,4,6,8]
llists:filter filters the elements of the list based on conditions.
- 9> Fruit = [apple, pear, orange]. %创建一个列表
- [apple,pear,orange]
- 10> MakeTest = fun(L)->(fun(X)->lists:member(X,L) end) end.%创建一个测试函数。
- #Fun<erl_eval.6.52032458>
- 11> IsFruit = MakeTest(Fruit).%这里不是函数声明,而是匹配了MakeTest的返回值。
- #Fun<erl_eval.6.52032458>
- 12> IsFruit(pear).%调用函数
- true
- 13> lists:filter(IsFruit, [dog, orange, cat, apple, bear]).%过滤
- [orange,apple]
A function is declared within MakeTest because it is the last statement and is therefore used as a return value.
Add a function to the module
- -module(test). %模块声明,模块名必须与文件名相同。
- -export([area/1,test/0,for/3]). %导出声明,声明可以外部使用的函数
- area({rectangle, Width, Height}) -> Width*Height; %子句
- area({square, Side}) -> Side * Side.
- test() ->
- 12 = area({rectangle, 3, 4}),
- 144 = area({square, 13}),
- tests_worked.