Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Erlang modules and functions


May 13, 2021 Erlang



Modules and functions

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.

Create modules and call functions:

The module is the basic unit of erlang.
The module is saved in a file with the extension .erl. Y ou must compile before you can run, and the compiled module has .beam as its extension.
If the clause does not return a statement, the value of the last expression is the return value.

  1. -module(geometry). %模块声明,模块名必须与文件名相同。
  2. -export([area/1]). %导出声明,声明可以外部使用的函数
  3. area({rectangle, Width, Height}) -> Width*Height; %子句1
  4. area({square, Side}) -> Side * Side.%子句2

This function area has multiple clauses, which are used between clauses; S eparate.

Compile

In the console, you can compile geometry.erl using c(geometry).
The corresponding geometry.beam file is generated in the current directory.

  1. 17> c("ErlangGame/geometry.erl").
  2. 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.
  3. {ok,geometry}

Path

The parameter of c is the file name. A ll with the extension .erl. I s the absolute path, relative path, can be.
For example, my directory is e: / mywokespace / ErlangGame / geometry.erl

  1. c("e:/mywokespace/ErlangGame/geometry.erl").%使用绝对路径
  2. c("ErlangGame/geometry.erl").%使用相对路径,这个时候我所在的目录是e:/mywokespace/
  3. 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.

fun: Basic abstraction unit

Define a function

  1. 1> Double = fun(x)->2*x end.
  2. #Fun<erl_eval.6.52032458>
  3. 2> Double(2).
  4. ** exception error: no function clause matching
  5. 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.

  1. 3> Three = fun(X)-> 3 * X end.
  2. #Fun<erl_eval.6.52032458>
  3. 4> Three(2).
  4. 6

Ok. I t worked.

Functions can be used as arguments

  1. 5> L = [1,2,3,4].
  2. [1,2,3,4]
  3. 6> lists:map(Three, L).
  4. [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


The test is equal.

  1. 8> lists:filter(fun(X)->(X rem 2)=:=0 end,[1,2,3,4,5,6,7,8]).
  2. [2,4,6,8]

llists:filter filters the elements of the list based on conditions.

The function is the return value

  1. 9> Fruit = [apple, pear, orange]. %创建一个列表
  2. [apple,pear,orange]
  3. 10> MakeTest = fun(L)->(fun(X)->lists:member(X,L) end) end.%创建一个测试函数。
  4. #Fun<erl_eval.6.52032458>
  5. 11> IsFruit = MakeTest(Fruit).%这里不是函数声明,而是匹配了MakeTest的返回值。
  6. #Fun<erl_eval.6.52032458>
  7. 12> IsFruit(pear).%调用函数
  8. true
  9. 13> lists:filter(IsFruit, [dog, orange, cat, apple, bear]).%过滤
  10. [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

  1. -module(test). %模块声明,模块名必须与文件名相同。
  2. -export([area/1,test/0,for/3]). %导出声明,声明可以外部使用的函数
  3. area({rectangle, Width, Height}) -> Width*Height; %子句
  4. area({square, Side}) -> Side * Side.
  5. test() ->
  6. 12 = area({rectangle, 3, 4}),
  7. 144 = area({square, 13}),
  8. tests_worked.