xexexe

Coding helloworld by erlang

把erlang的helloworld代码敲了两遍,对erlang的理解有了初步的认识,比如,erlang的尾递归,代替循环之类。我把我敲的erlang的helloworld代码贴出来先,后面有时间,我再来补点注释。

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
-module(helloworld).
-export([hw/0]).
-export([hw3/1]).
-export([hw5/1]).
-export([hw6/1]).
-export([hw7/1]).
-export([hw8/1]).
-export([hw9/1]).
-export([hw10/0]).
-export([hw11/0]).
-export([hw12/0]).
-export([hw14/0]).
-export([hw13/0]).
-export([hw15/0]).
-export([hw16/0]).
-export([hw16b/0]).
-export([hw18/0]).
hw() -> io:format("Hello World!! ~n").
hw3(N) when N > 0 ->
io:format("Hello World!! ~n"),
hw3(N-1);
hw3(0) -> ok.
hw5(N) ->
case N of
X when X > 0 ->
io:format("Hello World ~n"),
hw5(N-1);
0 -> ok
end.
hw6(N) ->
if
N > 0 ->
io:format("Hello World ~n"),
hw5(N-1);
N == 0 ->
ok
end.
hw7(N) ->
[hw() || _ <- lists:seq(1, N) ].
for(I, N, F) when I < N ->
F(),
for(I + 1, N, F);
for(N, N, F) ->
F().
hw8(N) ->
for(1, N, fun hw/0).
hw9(N) ->
for(1, N, fun() -> hw() end).
hw(Who) ->
io:format("Hello ~p ~n", [Who]).
hw10(L) ->
[hw(X) || X <- L].
hw10() ->
L = ["ali", "mark", "bob", "cat"],
hw10(L).
hw11(L) ->
lists:map(fun hw/1, L).
hw11() ->
L = ["mac", "ios", "dog", "anne"],
hw11(L).
hw14(L) ->
lists:foreach(fun hw/1, L).
hw14() ->
L = ["hall", "book", "cup"],
hw14(L).
hw12([H|T]) ->
hw(H),
hw12(T);
hw12([]) ->
ok.
hw12() ->
L = ["teacher", "student", "Head"],
hw12(L).
hw13(L) ->
case L of
[] -> ok;
[H|T] -> hw(H),
hw13(T)
end.
hw13() ->
L = ["iPhone", "android", "eye", "hand"],
hw13(L).
hw15srv() ->
receive
{name, Name} ->
io:format("Hello ~p ~n", [Name]),
hw15srv()
end.
hw15() ->
Pid = spawn(fun hw15srv/0),
Pid ! {name, "alice"},
Pid ! {name, "Bob"}.
hw16srv() ->
receive
{From, {name, Name}} ->
io:format("Server recive: ~p ~n", [Name]),
From ! {self(), {reply, "Welcome, " ++ Name}},
hw16srv()
end.
hw16() ->
Pid = spawn(fun hw16srv/0),
Sid = self(),
Pid ! {Sid, {name, "alice"}},
receive
{Pid, {reply, Msg}} ->
io:format("Client receive: ~p ~n", [Msg])
end.
rpc(Pid, Request) ->
Pid ! {self(), Request},
receive
{Pid, Response} ->
Response
end.
hw16b() ->
Pid = spawn(fun hw16srv/0),
{reply, Reply} = rpc(Pid, {name, "alice"}),
io:format("Client receive: ~p ~n", [Reply]).
rpc2(Pid, Request) ->
Ref = make_ref(),
Pid ! {{self(), Ref}, Request},
receive
{Ref, Reply} ->
Reply
end.
hw18srv() ->
receive
{{From, Ref}, {name, Name}} ->
io:format("Server receive: ~p ~n", [Name]),
From ! {Ref, {reply, "Welcome, " ++ Name}},
hw16srv()
end.
hw18() ->
Pid = spawn(fun hw18srv/0),
{reply, Reply} = rpc2(Pid, {name, "alice"}),
io:format("Client receive: ~p ~n", [Reply]).