the basic knowledge
- the Erlang shell inside Erlang software sets accept bits of Erlang code to be executed and evaluated,to input the
erl
command can start that shell in OS. - the combination key of C and Control used to stop Erlang and shell as follows.
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
(v)ersion (k)ill (D)b-tables (d)istribution
- a module and functions in the moudle can be easily constructed.
- for example,you put the following codes into the
hello.erl
file for defininghello
moudle andadd
function firstly.
-module(hello).
-export([add/2]).
add(X,Y) ->
X + Y.
# referen
secondly, import the hello
moudle and call the add
functoin as follows.
> file:set_cwd("E:/learn/lerl").
ok
> c(hello).
{ok,hello}
> hello:add(11,22).
33
- the export statement claims some functions for exporting them to be used by other moudle through the
-import
indication or the calling ofModule:Function
.
-module(my_module).
-import(lists, [map/2, filter/2]). % 导入 lists 模块的 map/2 和 filter/2
my_func(List) ->
map(fun(X) -> X * 2 end, List). % 可以直接调用 map,而不用 lists:map
-module(my_module).
my_func(List) ->
lists:map(fun(X) -> X * 2 end, List). % 显式调用 lists:map
in addtion, to compile the moudle that need to import in Erlang Shell as follows,that perhaps be more convenient.
> c(hello).
{ok,hello}
- Atom in Erlang,which starts with a small letter,just is a constant name that is different from variable which has value.for example:
> X=5.
5
> X.
5
> x.
x
- tuple is a combinated data type, which consists of elements, that can belong to either same type or various types such as
{inch, Y}
.one tuple data can be nested in another tuple data such as{paris, {f, 28}}
. - List in Erlang represent as a sets of elements surrounded by square brackets on both sides such as
[1,as,X]
. the|
operation can seprates List into two parts such as[Head|Remain]= [1,2,3,4,5]
,as result of running that code , the Head is1
and Remain is[2,3,4,5]
.
|
just splits a list from beginning and not elsewhere.- if you want to get the first one and the second one,you have to use two variable to grasp the first two items in a list.for example:
[Oneitem,Twoitem|Remain]= [1,2,3,4,5]
the following codes demostrates how to get the length of list
-module(hello).
-export([get_list_len/1]).
get_list_len([]) ->
0;
get_list_len([Head | Remain]) ->
1 + list_length(Remain).