server_1.erl
[deep@deep erlangstudy]$ cat server_1.erl
-module(server_1).
-export([start/1,loop/1]).
start(Dir)->spawn(server_1,loop,[Dir]).
loop(Dir)->
receive
{Client,list_dir}->
Client!{self(),file:list_dir(Dir)};
{Client,{list_dir,Mydir}}->
Client!{self(),file:list_dir(Mydir)};
{Client,{get_file,File}}->
Fullname=filename:join(Dir,File),
Client!{self(),file:read_file(Fullname)}
end,
loop(Dir).
client_1.erl
[deep@deep erlangstudy]$ cat client_1.erl
-module(client_1).
-export([ls/1,get_file/2,list_dir/2]).
ls(Server)->
Server!{self(),list_dir},
receive
{Server,Filelist}->
Filelist
end.
get_file(Server,File)->
Server!{self(),{get_file,File}},
receive
{Server,Content}->
Content
end.
list_dir(Server,Dir)->
Server!{self(),{list_dir,Dir}},
receive
{Server,Mydir}->
Mydir
end.
匹配
Student={student,{1,{name,zhangsang},{age,28}},{2,{name,wangwu},{age,29}}}.
4> {student,{_,{_,Name1},{_,Age1}},{_,{_,Name2},{_,Age2}}}=Student.
{student,{1,{name,zhangsang},{age,28}},
{2,{name,wangwu},{age,29}}}
5> Name1.
zhangsang
6> Age1.
28
7> Name2.
wangwu
8> Age2.
29
14> Stdx={student,[{1,{name,zhangsang},{age,28}},{2,{name,wangwu},{age,29}}]}.
{student,[{1,{name,zhangsang},{age,28}},
{2,{name,wangwu},{age,29}}]}
15> {student,Stdinfo}=Stdx.
{student,[{1,{name,zhangsang},{age,28}},
{2,{name,wangwu},{age,29}}]}
16> Stdinfo.
[{1,{name,zhangsang},{age,28}},{2,{name,wangwu},{age,29}}]
17> [Mystd1|_]=Stdinfo.
[{1,{name,zhangsang},{age,28}},{2,{name,wangwu},{age,29}}]
18> Mystd1
18> .
{1,{name,zhangsang},{age,28}}
19> {No,{name,Names1},{age,Ages1}}.
* 1: variable 'No' is unbound
20> {No,{name,Names1},{age,Ages1}}=Mystd1.
{1,{name,zhangsang},{age,28}}
21> Names1
21> .
zhangsang
22> Ages1.
28