-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeometry.ex
More file actions
44 lines (36 loc) · 766 Bytes
/
geometry.ex
File metadata and controls
44 lines (36 loc) · 766 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
defmodule Geometry do
defmodule Rectangle do
def rectangle_area(a, b) do
a * b
end
def square_area(a) do
rectangle_area(a, a)
end
end
defmodule Circle do
@moduledoc "Implements basic circle functions"
@pi 3.14159
@doc "Computes the area of a circle"
def area(r), do: r * r * @pi
@doc "Computes the circumference of a circle"
def circumference(r), do: 2 * r * @pi
end
def area({:rectangle, a, b}) do
a * b
end
def area({:square, a}) do
a * a
end
def area({:circle, r}) do
r * r * 3.14
end
def area(unknown) do
{:error, {:unknown_shape, unknown}}
end
def fattoriale(n) when n == 1 do
n
end
def fattoriale(n) when n > 1 do
n * fattoriale(n - 1)
end
end