-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguards.erl
More file actions
29 lines (24 loc) · 737 Bytes
/
Copy pathguards.erl
File metadata and controls
29 lines (24 loc) · 737 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
-module(guards).
-export([factorial/1, range/1]).
-export([is_domestic/1]).
-define(DOMESTIC_ANIMALS, [cat, dog, horse]).
%Guards always resolve till true/false
%factiorial without guards
%factorial(0) ->
% 1;
%factorial(1) ->
% 1;
%factorial(N) ->
% N * factorial(N - 1).
%factorial with guards
factorial(N) when N < 2 ->
1; % | GUARD |
factorial(N) ->
N * factorial(N - 1).
%range(X) when is_integer(X), X > 10, X < 100 -> ALL guards must be true
range(X) when is_integer(X); X > 10; X < 100 -> % Only one guard need to be true (guard sequece)
io:format("~w is in range\n", [X]);
range(X) ->
io:format("~w is not in range\n", [X]).
is_domestic(Animal) ->
lists:member(Animal, ?DOMESTIC_ANIMALS).