-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathprefer_pm.erl
More file actions
43 lines (39 loc) · 818 Bytes
/
Copy pathprefer_pm.erl
File metadata and controls
43 lines (39 loc) · 818 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
-module(prefer_pm).
-export ([good/3, bad/3]).
%% @doc Uses equality comparisons (=:=) for everything
-spec bad(T, T, 0|1|2) -> ok.
bad(A, B, 0) ->
case A =:= B of
true -> proceed();
false -> fail(A)
end;
bad(A, B, 1) ->
case change(A) =:= B of
true -> proceed();
false -> fail(A)
end;
bad(A, B, 2) ->
case change(A) =:= change(B) of
true -> proceed();
false -> fail(A)
end.
%% @doc Uses pattern-matching everywhere
-spec good(T, T, 0|1|2) -> ok.
good(A, B, 0) ->
case A of
B -> proceed();
A -> fail(A)
end;
good(A, B, 1) ->
case change(A) of
B -> proceed();
C -> fail(C)
end;
good(A, B, 2) ->
case {change(A), change(B)} of
{C, C} -> proceed();
{D, _} -> fail(D)
end.
change(X) -> {changed, X}.
proceed() -> ok.
fail(E) -> exit({error, E}).