当前位置:网站首页>Erlang学习01
Erlang学习01
2022-07-24 10:12:00 【Tsuit】
下载和配置Erlang环境移步到其他文章,这里只讲基本操作和概念
启动Erlang命令,打开CMD输入erl
>erl
Eshell V8.3 (abort with ^G)
1>
停止Erlang Shell命令为Ctrl+C,立刻停止系统可执行表达式halt()
在Erlang中所有变量都必须以大写字母开头,并且Erlang与其他语言它的变量是一次性赋值变量,不能再试图在被设置后改变它的值否则会报错,在Erlang里,变量只不过是对某个值的引用:Erlang的实现方式用指针代表绑定变量,指向一个包含值的存储区。这个值不能被修改。
>erl
Eshell V8.3 (abort with ^G)
1> X = 20.
20
2> X =30.
** exception error: no match of right hand side value 30
和其他语言不一样的地方还有在Java中结尾一般使用;在Erlang中使用. 作为结尾(.+空格/Tab/回车)
浮点数
在Erlang无论是否整除只要使用了运算符/得到的结果都会是浮点数,例如:
>erl
Eshell V8.3 (abort with ^G)
1> 4/2.
2.0
div是取整数,rem是取余数。
1> 5 div 3.
1
2> 5 rem 3.
2
原子
在Erlang里,原子是全局性的,而且不需要宏定义或包含文件就能实现,原子以小写字母开头,后接一串字母、数字、下划线(_)或at(@)符号,原子还可以放在单引号(')内,因此'a'和a的意思完全一致。可以用这种引号形式创建以大写字母开头(否则会被解释成变量)或包含字母数字以外字符的原子,在某些语 言里,单引号和双引号可以互换使用。Erlang里不是这样。单引号的用法如前面所示,双引号用 于给字符串字面量(string literal)定界。
一个原子的值就是它本身。所以,如果输入一个原子作为命令,Erlang shell就会打印出这个原子的值。
>erl
Eshell V8.3 (abort with ^G)
1> hello.
hello
如果想把一些数量固定的项目归组成单一的实体,就会使用元组。Erlang没有类型声明,因此要创建一个“坐标点”,只需要这么写。
1> P = {10, 45}.
{10,45}
2> P.
{10,45}
为了更容易记住元组的用途,一种 常用的做法是将原子作为元组的第一个元素,用它来表示元组是什么。
1> F = {firstName, jok}.
{first,jok}
2> L = {lastName,armstrong}.
{last,armstrong}
3> P = {preson, F, L}.
{preson,{firstName,jok},{lastName,armstrong}}
如果有一个复杂的元组,就可以编写一个与该元组形状(结构)相同的模式,并在待提取值 的位置加入未绑定变量来提取该元组的值。
1> Point1={point,25,25}.
{point,25,25}
2> {point,C,C}=Point1.
{point,25,25}
3> C.
25
将_作为占位符,用于表示不感兴趣的那些变量。符号_被 称为匿名变量。与正规变量不同,同一模式里的多个_不必绑定相同的值。
1> Person={person,{name,joe,armstrong},{footsize,42}}.
{person,{name,joe,armstrong},{footsize,42}}
2> {_,{_,Who,_},_}=Person.
{person,{name,joe,armstrong},{footsize,42}}
3> Who.
joe
列表
列表(list)被用来存放任意数量的事物。创建列表的方法是用中括号把列表元素括起来, 并用逗号分隔它们。
列表的第一个元素被称为列表头。假设把列表头去掉,剩下的就被称为列表尾,列表头可以是任何事物,但列表尾通常仍然是个列表。
如果T是一个列表,那么[H|T]也是一个列表,它的头是H,尾是T。竖线(|)把列表的头与 尾分隔开。[]是一个空列表。
1> ThingsToBuy=[{orange,4},{pears,6},{milk,3}].
[{orange,4},{pears,6},{milk,3}]
2> ThingsToBuy1=[{orange,4},{newspaper,1}|ThingsToBuy].
[{orange,4},{newspaper,1},{orange,4},{pears,6},{milk,3}]
3> [Buy1|ThingsToBuy2]=ThingsToBuy1.
[{orange,4},{newspaper,1},{orange,4},{pears,6},{milk,3}]4> [Buy2,Buy3|ThingsToBuy3]=ThingsToBuy2.
[{newspaper,1},{orange,4},{pears,6},{milk,3}]
如果有一个非空列表 L,那么表达式[X|Y] = L(X和Y都是未绑定变量)会提取列表头作为X,列表尾作为Y。
字符串
严格来说,Erlang里没有字符串。要在Erlang里表示字符串,可以选择一个由整数组成的列表或者一个二进制型。在一些编程语言里,字符串可以用单引号或双引号定界,而在Erlang里,必须使用双引号。
1> Name="hello".
"hello"
当shell打印某个列表的值时,如果列表内的所有整数都代表可打印字符,它就会将其打印成 字符串字面量。否则,打印成列表记法。
1> X=[97,98,99].
"abc"2> I=$a.
97
f()命令让shell忘记现有的任何绑定。在这个命令之后,所有变量都会变成未绑定状态
模块
模块是Erlang的基本代码单元。
-module(geometry).
-export([area/1]).
area({rectangle,Width,Height}) -> Width * Height;
area({square,Side}) ->Side * Side.文件的第一行是模块声明。声明里的模块名必须与存放该模块的主文件名相同。
第二行是导出声明。Name/N这种记法是指一个带有N个参数的函数Name,N被称为函数的元数。export的参数是由Name/N项目组成的一个列表。因此,-export([area/1])的意思 是带有一个参数的函数area可以在此模块之外调用。未从模块里导出的函数只能在模块内调用。已导出函数就相当于面向对象编程语言(OOPL) 里的公共方法,未导出函数则相当于OOPL里的私有方法
1> c(geometry).
{ok,geometry}
2> geometry:area({rectangle,10,15}).
150
3> geometry:area({square,3}).
9
给代码添加测试
-module(geometry1).
-export([test/0, area/1]).
test() ->
12 = area({rectangle,3,4}),
144 = area({square,12}),
tests_worked.
area({rectangle,Width,Height}) -> Width * Height;
area({square,Side}) ->Side * Side.1> c(geometry1).
{ok,geometry1}
2> geometry1:test().
tests_worked
12 = area({rectangle, 3, 4})这行代码是一项测试。如果area({rectangle, 3, 4}) 没有返回12,模式匹配就会失败,我们会得到一条错误消息。执行geometry1:test()并看到结 果是tests_worked时,就可以确定test/0主体里的所有测试都成功通过了。
边栏推荐
- When the hot tea is out of stock, what does the new tea drink rely on to continue its life?
- [STM32 learning] (9) stm32f1 general timer realizes simple breathing lamp
- 聚集日志服务器
- AttributeError: module ‘sipbuild. api‘ has no attribute ‘prepare_ metadata_ for_ build_ wheel‘
- String__
- [STM32 learning] (18) STM32 realizes LCD12864 display - parallel implementation of 8-bit bus
- Anti shake and throttling
- [STM32 learning] (11) STM32 Mifare_ Use of one (S50) m1s50 (read, write, key modification, control bit interpretation)
- zoj 2770 差分约束系统---2--2022年5月20日
- 757. Set the intersection size to at least 2: greedy application question
猜你喜欢

Do you really understand the concept of buffer? Take you to uncover the buffer zone~

zoj 2770 差分约束系统---2--2022年5月20日
![[STM32 learning] (18) STM32 realizes LCD12864 display - parallel implementation of 8-bit bus](/img/6b/b49b9e7fff62026a4818561d79fe3f.png)
[STM32 learning] (18) STM32 realizes LCD12864 display - parallel implementation of 8-bit bus

CRC Coding in C language

Detailed explanation of uninstalling MySQL completely under Linux
![[STM32 learning] (12) STM32 realizes LCD1602 simple static reality](/img/78/954ebaae0cce5d9387e7032fa85e60.png)
[STM32 learning] (12) STM32 realizes LCD1602 simple static reality

An article takes you to understand the operation of C language files in simple terms

二叉树、二叉树排序树的实现及遍历

MySQL query database capacity size

Add SSH key to bitbucket
随机推荐
Deployment and analysis of coredns
2022 trusted cloud authoritative assessment released: Tianyi cloud has obtained ten certifications and five best practices
CRC Coding in C language
【机器人学习】机构运动学分析与matlab仿真(三维模型+word报告+matlab程序)
[STM32 learning] (10) stm32f1 general timer realizes pulse counter
System a uses window.open to call system B, and system B carries data back to system a after processing the business
分布式锁-Redission 原理分析
String__
Balance between management / business and technology
Home raiding III (leetcode-337)
How to solve command 'xxx GCC' not found, but can be installed with:??
[robot learning] mechanism kinematics analysis and MATLAB simulation (3D model +word report +matlab program)
Spark Learning: implement compact table command
ThreeJs
unity中物体z旋转同步面板上的数值
Installation UMI tutorial (error reporting and solutions)
Implementation and traversal of binary tree and binary tree sorting tree
Where is the bitbucket clone address
zoj 2770 差分约束系统---2--2022年5月20日
Scan line, weight segment tree