-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsnippets.erl
More file actions
52 lines (31 loc) · 771 Bytes
/
snippets.erl
File metadata and controls
52 lines (31 loc) · 771 Bytes
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
-module(snippets).
-compile(export_all).
area({circle,_,R}) ->
math:pi()*R*R;
area({tri,_,A,B,C}) ->
S = (A+B+C)/2,
math:sqrt(S*(S-A)*(S-B)*(S-C)).
echo(Pid,N) ->
receive
Msg -> Pid!Msg
end,
echo(Pid,N+1).
-spec switch(number(),T,T) -> T.
switch(N,Pos,Neg) ->
case N>0 of
true -> Pos;
_ -> Neg
end.
-spec sum_first_two(list(number())) -> number().
sum_first_two([A,B|_Rest])
-> A+B.
-spec lswitch(number(),fun(() ->T),fun(() ->T)) -> T.
lswitch(N,Pos,Neg) ->
case N>0 of
true -> Pos();
_ -> Neg()
end.
lex1() -> lswitch(1,fun() -> 3+4 end,fun () -> 1/0 end).
-define(switch(N,Pos,Neg),
lswitch(N,fun() -> Pos end,fun() -> Neg end)).
lex2() -> ?switch(1,3+4,1/0).