Erlang notes[1]

the basic knowledge

  1. 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.
  2. 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
  1. a module and functions in the moudle can be easily constructed.
  2. for example,you put the following codes into the hello.erl file for defining hello moudle and add 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
  1. the export statement claims some functions for exporting them to be used by other moudle through the -import indication or the calling of Module: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}
  1. 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
  1. 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}}.
  2. 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 is 1 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).

references

  1. https://www.erlang.org/doc/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翻译之海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值