-
Notifications
You must be signed in to change notification settings - Fork 1
/
hello.erl
83 lines (62 loc) · 1.82 KB
/
hello.erl
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
-module(hello).
-compile(export_all).
-compile(nowarn_export_all).
-compile(nowarn_unused_vars).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Lists
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
append([H|T], Tail) ->
[H|append(T, Tail)];
append([], Tail) ->
Tail.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Numeric types
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sum ([]) -> 0;
sum ([X|Xs]) -> X + sum(Xs).
average(Xs) -> sum(Xs) / length(Xs).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Algebaric Data Types
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-type tree(A) :: nil
| {node, A, tree(A), tree(A)}.
findNode(_,nil) ->
false;
findNode(N,{node,N,Lt,Rt}) ->
true;
findNode(N,{node,_,Lt,Rt}) ->
findNode(N, Lt) or findNode(N,Rt).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Overloaded constructors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-type mlist(A) :: nil | {cons, A, mlist(A)}.
empty () -> nil.
flattenTree(nil) ->
[];
flattenTree({node,N,Lt,Rt}) ->
flattenTree(Lt) ++ [N|flattenTree(Rt)].
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Messaging
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-type request() :: {ping, pid()}
| {get_sum,pid(),integer(),integer()}.
server() ->
receive
{ping, Ping_PID} ->
Ping_PID ! {pong, self()};
{get_sum, Pong_PID, X, Y} ->
Pong_PID ! {sum, X + Y}
end,
server().
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Records
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-record(person,{
name = "nachi" :: [char()]
, phone :: integer()
, id
}).
createPerson() -> #person{phone = 1235, id = 0}.
updatePresonId(P,Id) -> P#person{id = Id}.
getId(P) -> P#person.id.
getPhone(P) -> P#person.phone.