当前位置:网站首页>Erlang learning 01
Erlang learning 01
2022-07-24 10:17:00 【Tsuit】
Download and configure Erlang Environment moves to other articles , Here we only talk about basic operations and concepts
start-up Erlang command , open CMD Input erl
>erl
Eshell V8.3 (abort with ^G)
1>
stop it Erlang Shell The order is Ctrl+C, Immediately stop the system executable expression halt()
stay Erlang All variables in the must begin with Capital start , also Erlang Unlike other languages, its variables are one-time assignment variables , You can no longer try to change its value after it is set, otherwise an error will be reported , stay Erlang in , A variable is simply a reference to a value :Erlang The implementation of the method uses pointers to represent bound variables , Point to a store containing values . This value cannot be modified .
>erl
Eshell V8.3 (abort with ^G)
1> X = 20.
20
2> X =30.
** exception error: no match of right hand side value 30
Different from other languages, there are also Java The middle end is generally used ; stay Erlang Use in . As the end (.+ Space /Tab/ enter )
Floating point numbers
stay Erlang No matter whether it is divisible or not, as long as the operator is used / The results will be floating point numbers , for example :
>erl
Eshell V8.3 (abort with ^G)
1> 4/2.
2.0
div Is to take an integer ,rem It's the remainder .
1> 5 div 3.
1
2> 5 rem 3.
2
atom
stay Erlang in , Atoms are global , And it can be realized without macro definition or include file , Atoms are represented by Lowercase letters start , Followed by a string of letters 、 Numbers 、 Underline (_) or at(@) Symbol , Atoms can also be placed in single quotation marks (') Inside , therefore 'a' and a Means exactly the same . You can use this quotation mark form to create a quotation mark that begins with a capital letter ( Otherwise it will be interpreted as a variable ) Or atoms containing characters other than alphanumeric , In some languages Yan Li , Single quotation marks and double quotation marks can be used interchangeably .Erlang That's not the case in the library . The usage of single quotation marks is shown above , Use double quotation marks To give a string literal (string literal) Delimit .
The value of an atom is itself . therefore , If you enter an atom as a command ,Erlang shell Will print out the value of this atom .
>erl
Eshell V8.3 (abort with ^G)
1> hello.
hello
If you want to group some fixed number of items into a single entity , Will use tuples .Erlang No type declaration , So create a “ Coordinates ”, Just write it like this .
1> P = {10, 45}.
{10,45}
2> P.
{10,45}
To make it easier to remember the purpose of tuples , A kind of The common practice is to take the atom as the first element of the tuple , Use it to express what a tuple is .
1> F = {firstName, jok}.
{first,jok}
2> L = {lastName,armstrong}.
{last,armstrong}
3> P = {preson, F, L}.
{preson,{firstName,jok},{lastName,armstrong}}
If there is a complex tuple , You can write a shape with the tuple ( structure ) Same pattern , And the value to be extracted Add unbound variables to the position of to extract the value of the tuple .
1> Point1={point,25,25}.
{point,25,25}
2> {point,C,C}=Point1.
{point,25,25}
3> C.
25
take _ As placeholder , Used to represent those variables that are not of interest . Symbol _ By Called anonymous variables . Different from normal variables , Multiple in the same mode _ You don't have to bind the same value .
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
list (list) Used to store any number of things . The way to create a list is to enclose the list elements with square brackets , And separate them with commas .
The first element of the list is called the list header . Suppose you remove the list header , The rest is called the end of the list , The list header can be anything , But the end of the list is usually still a list .
If T It's a list , that [H|T] It's also a list , Its head is H, The tail is T. A vertical bar (|) Match the header of the list with The tail is separated .[] It's an empty list .
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}]
If there is a non empty list L, So the expression [X|Y] = L(X and Y Are unbound variables ) The list header will be extracted as X, The end of the list is Y.
character string
Strictly speaking ,Erlang There is no string in . To be in Erlang Inside represents a string , You can choose a list of integers or a binary . In some programming languages , Strings can be delimited with single or double quotation marks , And in the Erlang in , You must use double quotes .
1> Name="hello".
"hello"
When shell When printing the value of a list , If all integers in the list represent printable characters , It will print it as Literal of a string . otherwise , Print as a list notation .
1> X=[97,98,99].
"abc"2> I=$a.
97
f() Order to let shell Forget any existing bindings . After this command , All variables will become unbound
modular
The module is Erlang Basic code unit .
-module(geometry).
-export([area/1]).
area({rectangle,Width,Height}) -> Width * Height;
area({square,Side}) ->Side * Side. The first line of the file is the module declaration . The module name in the declaration must be the same as the main file name where the module is stored .
The second line is the export declaration .Name/N This notation refers to a with N A function of parameters Name,N It is called the element of a function .export The parameters of are determined by Name/N A list of items . therefore ,-export([area/1]) It means Is a function with one parameter area Can be called outside this module . Functions not exported from the module can only be called within the module . Exported functions are equivalent to object-oriented programming languages (OOPL) Public methods in , An unexported function is equivalent to OOPL Private methods in
1> c(geometry).
{ok,geometry}
2> geometry:area({rectangle,10,15}).
150
3> geometry:area({square,3}).
9
Add tests to the code
-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}) This line of code is a test . If area({rectangle, 3, 4}) No return 12, Pattern matching will fail , We will get an error message . perform geometry1:test() And see the knot Fruit is tests_worked when , You can be sure test/0 All tests in the main body have passed successfully .
边栏推荐
- Mysql database JDBC programming
- Raspberry Pie: [failed] failed to start /etc/rc local Compatibility.
- zoj-Swordfish-2022-5-6
- Deployment and analysis of coredns
- Arduino drive lcd1602a
- Mysql8.0 authorized remote login
- Curse of knowledge
- Interpretation of websocket protocol -rfc6455
- [STM32 learning] (7) use of serial port 2 (usart2)
- Add SSH key to bitbucket
猜你喜欢

【二叉树先导】树的概念和表示方法
![[STM32 learning] (16) STM32 realizes LCD1602 display (74HC595 drive) - 4-bit bus](/img/8f/19b0eb959d2b3f896c8e99f8e673d1.png)
[STM32 learning] (16) STM32 realizes LCD1602 display (74HC595 drive) - 4-bit bus

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

图模型2--2022-5-13

The concept and representation of a tree

Dr. water 3

MySQL 数据库 JDBC编程

Jenkins deploys the project and prompts that the module package defined by him cannot be found
![Calculate CPU utilization [Prometheus]](/img/00/d9f297e3013cabbf3d41be58105fc7.png)
Calculate CPU utilization [Prometheus]

NiO knowledge points
随机推荐
[STM32 learning] (12) STM32 realizes LCD1602 simple static reality
Android uses JDBC to connect to a remote database
cannot unpack non-iterable NoneType object
Erlang学习番外
JS string method
二叉树、二叉树排序树的实现及遍历
Raspberry Pie: serial port login does not display print information
Create a vertical seekbar from scratch
ffmpeg花屏解决(修改源码,丢弃不完整帧)
Home raiding II (leetcode-213)
unity中物体z旋转同步面板上的数值
Home raiding III (leetcode-337)
The best time to buy and sell stocks Ⅲ (leetcode-123)
In depth analysis of common cross end technology stacks of app
Where is the bitbucket clone address
ES6 question
Selnium checks three conditions when it cannot locate an element
MySQL 数据库 JDBC编程
[robot learning] mechanism kinematics analysis and MATLAB simulation (3D model +word report +matlab program)
Calculate CPU utilization [Prometheus]