CodeMan

Each of us must find a way to love the world. You have found yours.

0%

Programming Erlang exercise 3.10

1. 快速浏览3.1.3节,然后测试并记忆这些行编辑命令
命令 说明 助记法
^A 行首 A为第一个字母
^D 删除当前字符 D为Delete
^E 行尾 E为End
^F 向前的字符 F为Forward
^B 向后的字符 B为Back
^P 前一行 P为Preceding
^N 下一行 N为Next
^T 调换最近两个字符的位置 T为Transposition
Tab 尝试扩展当前模块或函数的名称

^在Mac中按键为:Control

2. 在shell里面输入help()命令。你将看到一长串命令。可以试一试其中一些命令。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
yfydemacbook-pro:opensource yfy$ erl
Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V7.2.1 (abort with ^G)
1> help().
** shell internal commands **
b() -- display all variable bindings
e(N) -- repeat the expression in query <N>
f() -- forget all variable bindings
f(X) -- forget the binding of variable X
h() -- history
history(N) -- set how many previous commands to keep
results(N) -- set how many previous command results to keep
catch_exception(B) -- how exceptions are handled
v(N) -- use the value of query <N>
rd(R,D) -- define a record
rf() -- remove all record information
rf(R) -- remove record information about R
rl() -- display all record information
rl(R) -- display record information about R
rp(Term) -- display Term using the shell's record information
rr(File) -- read record information from File (wildcards allowed)
rr(F,R) -- read selected record information from file(s)
rr(F,R,O) -- read selected record information with options
** commands in module c **
bt(Pid) -- stack backtrace for a process
c(File) -- compile and load code in <File>
cd(Dir) -- change working directory
flush() -- flush any messages sent to the shell
help() -- help info
i() -- information about the system
ni() -- information about the networked system
i(X,Y,Z) -- information about pid <X,Y,Z>
l(Module) -- load or reload module
lc([File]) -- compile a list of Erlang modules
ls() -- list files in the current directory
ls(Dir) -- list files in directory <Dir>
m() -- which modules are loaded
m(Mod) -- information about module <Mod>
memory() -- memory allocation information
memory(T) -- memory allocation information of type <T>
nc(File) -- compile and load code in <File> on all nodes
nl(Module) -- load module on all nodes
pid(X,Y,Z) -- convert X,Y,Z to a Pid
pwd() -- print working directory
q() -- quit - shorthand for init:stop()
regs() -- information about registered processes
nregs() -- information about all registered processes
uptime() -- print node uptime
xm(M) -- cross reference check a module
y(File) -- generate a Yecc parser
** commands in module i (interpreter interface) **
ih() -- print help for the i module
true
2>
3. 试着用一个元组来表示一座房子,再用一个房子列表来表示一条街道。请确保你能向这些结构中加入数据或从中取出数据。

Erlang的所有数据类型:Data Types

类型 说明
Terms A piece of data of any data type
Number two types of numeric literals, integers and floats
Atom An atom is a literal, a constant with name 如果是以大写字母开前头则需要用’’号括起来
Bit Strings and Binaries A bit string is used to store an area of untyped memory.
Reference A reference is a term that is unique in an Erlang runtime system
Fun A fun is a functional object.
Port Identifier A port identifier identifies an Erlang port.
Pid A process identifier, pid, identifies a process.
Tuple A tuple is a compound data type with a fixed number of terms.
Map A map is a compound data type with a variable number of key-value associations.
List A list is a compound data type with a variable number of terms.
String Strings are enclosed in double quotes (“), but is not a data type in Erlang.
Record A record is a data structure for storing a fixed number of elements.
Boolean There is no Boolean data type in Erlang. Instead the atoms true and false are used to denote Boolean values.
Escape Sequences 具体查看上面数据类型链接

房子:

House1={house,{owner,yangfy1},{location,’china cq1’}}

House2={house,{owner,yangfy2},{location,’china cq2’}}

House3={house,{owner,yangfy3},{location,’china cq3’}}

街道:

ShaZhengjie=[House1,House2,House3]

1
2
3
4
5
6
7
8
9
10
11
12
Eshell V7.2.1  (abort with ^G)
1> House1={house,{owner,yangfy1},{location,'china cq1'}}.
{house,{owner,yangfy1},{location,'china cq1'}}
2> House2={house,{owner,yangfy2},{location,'china cq2'}}.
{house,{owner,yangfy2},{location,'china cq2'}}
3> House3={house,{owner,yangfy3},{location,'china cq3'}}.
{house,{owner,yangfy3},{location,'china cq3'}}
4> ShaZhengjie=[House1,House2,House3].
[{house,{owner,yangfy1},{location,'china cq1'}},
{house,{owner,yangfy2},{location,'china cq2'}},
{house,{owner,yangfy3},{location,'china cq3'}}]
5>

读取House1中location;House2的owner

1
2
3
4
5
6
7
8
5> {house,_,{_,D}} = House1.
{house,{owner,yangfy1},{location,'china cq1'}}
6> D.
'china cq1'
7> {house,{_,O},_} = House2.
{house,{owner,yangfy2},{location,'china cq2'}}
8> O.
yangfy2

读取街道中第二栋房子的owner

1
2
3
4
5
6
7
8
9> [H1,H2|H3] = ShaZhengjie.
[{house,{owner,yangfy1},{location,'china cq1'}},
{house,{owner,yangfy2},{location,'china cq2'}},
{house,{owner,yangfy3},{location,'china cq3'}}]
10> {house,{_,O1},_}=H2.
{house,{owner,yangfy2},{location,'china cq2'}}
11> O1.
yangfy2

为街道新增加一栋房子

House4={house,{owner,yangfy4},{location,’china cq4’}}

1
2
3
4
5
6
7
8
9
10
12> House4={house,{owner,yangfy4},{location,'china cq4'}}.
{house,{owner,yangfy4},{location,'china cq4'}}
13> ShaZhengjie1 = [House4|ShaZhengjie].
[{house,{owner,yangfy4},{location,'china cq4'}},
{house,{owner,yangfy1},{location,'china cq1'}},
{house,{owner,yangfy2},{location,'china cq2'}},
{house,{owner,yangfy3},{location,'china cq3'}}]
16>