-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjungle.erl
48 lines (45 loc) · 1.64 KB
/
jungle.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
-module(jungle).
-export([greet/1, greet2/1, greet3/1]).
-spec greet({atom(), atom() | string(), number()}) -> ok.
greet(Animal) ->
case Animal of
{butterfly, _Name, Age} when 2 =< Age, Age =< 5 ->
io:format("Catch the butterfly!~n");
{monkey, Name, _Age} ->
io:format("Be careful on the branches, ~s!~n", [Name]);
{tiger, _Name, Age} when Age =< 2; 20 =< Age->
io:format("I'm not afraid of you!~n");
{tiger, _Name, _Age} ->
io:format("Run!~n");
{_Species, Name, _Age} ->
io:format("Hello ~s!~n", [Name]);
Other ->
throw({unexpected_value, Other})
end.
-spec greet2({atom(), atom() | string(), number()}) -> ok.
greet2(Animal = {_Species, _Name, Age}) when Age > 1000 ->
io:format("Warning: age is unrealistic of ~p~n", [Animal]);
greet2({Name, Name, _Age}) ->
io:format("You have a boring name.~n");
greet2({butterfly, _Name, Age}) when 2 =< Age, Age =< 5 ->
io:format("Catch the butterfly!~n");
greet2({monkey, Name, _Age}) ->
io:format("Be careful on the branches, ~s!~n", [Name]);
greet2({tiger, _Name, Age}) when Age =< 2; 20 =< Age->
io:format("I'm not afraid of you!~n");
greet2({tiger, _Name, _Age}) ->
io:format("Run!~n");
greet2({_Species, Name, _Age}) ->
io:format("Hello ~s!~n", [Name]);
greet2(Other) ->
throw({unexpected_value, Other}).
-spec greet3(atom()) -> ok.
greet3(Species) ->
if
Species =:= monkey ->
io:format("Be careful on the branches!~n");
Species =:= tiger ->
io:format("Run!~n");
true ->
io:format("Hello!~n")
end.