Chapter 3. Atoms, Tuples, and Pattern Matching

Étude 3-1: Pattern Matching

Use atoms and pattern matching to make your area function calculate the area of a rectangle, triangle, or ellipse. If your parameters are shape, a and b, the area for the atom :rectangle is a * b, where a and b represent the length and width. For a :triangle atom, the area is a * b / 2.0, with a and b representing the base and height of the triangle. For an :ellipse atom, the area is :math.pi() * a * b, where a and b represent the major and minor radiuses.

Here is some sample output.

iex(1)> c("geom.ex")
[Geom]
iex(2)> Geom.area(:rectangle, 3, 4)
12
iex(3)> Geom.area(:triangle, 3, 5)
7.5
iex(4)> Geom.area(:ellipse, 2, 4)
25.132741228718345

See a suggested solution in Appendix A.

Étude 3-2: Guards

Even though you won’t get an error message when calculating the area of a shape that has negative dimensions, it’s still worth guarding your area/3 function. You will want two guards for each pattern to make sure that both dimensions are greater than or equal to zero. Since both have to be non-negative, use and to separate your guards.

Here is some sample output.

iex(1)> c("geom.ex")
/Users/elixir/code/ch03-02/geom.ex:1: redefining module Geom
[Geom]
iex(2)> Geom.area(:rectangle, -3, 4)
** (FunctionClauseError) no function clause matching: Geom.area(:rectangle, -3, 4)
    /Users/elixir/code/ch03-02/geom.ex:21: Geom.area(:rectangle, -3, 4)
    erl_eval.erl:569: :erl_eval.do_apply/6
    src/elixir.erl:133: :elixir.eval_forms/3
    /bin/elixir/lib/iex/lib/iex/server.ex:19: IEx.Server.do_loop/1

iex(2)> Geom.area(:triangle, 3, -4)
** (FunctionClauseError) no function clause matching: Geom.area(:triangle, 3, -4)
    /Users/elixir/code/ch03-02/geom.ex:21: Geom.area(:triangle, 3, -4)
    erl_eval.erl:569: :erl_eval.do_apply/6
    src/elixir.erl:133: :elixir.eval_forms/3
    /bin/elixir/lib/iex/lib/iex/server.ex:19: IEx.Server.do_loop/1

iex(2)> Geom.area(:ellipse, -3, -4)
** (FunctionClauseError) no function clause matching: Geom.area(:ellipse, -3, -4)
    /Users/elixir/code/ch03-02/geom.ex:21: Geom.area(:ellipse, -3, -4)
    erl_eval.erl:569: :erl_eval.do_apply/6
    src/elixir.erl:133: :elixir.eval_forms/3
    /bin/elixir/lib/iex/lib/iex/server.ex:19: IEx.Server.do_loop/1
iex(2)> Geom.area(:rectangle, 3, 4)
12
iex(3)> Geom.area(:triangle, 3, 4)
6.0
iex(4)> Geom.area(:ellipse, 3, 4)

See a suggested solution in Appendix A.

Étude 3-3: Underscores

If you enter a shape that area/3 doesn’t know about, or if you enter negative dimensions, Elixir will give you an error message. Use underscores to create a “catch-all” version, so that anything at all that doesn’t match a valid rectangle, triangle, or ellipse will return zero. This goes against the Elixir philosophy of “let it fail,” but let’s look the other way and learn about underscores anyway.

Here is some sample output.

iex(1)> c("geom.ex")
[Geom]
iex(2)> Geom.area(:rectangle, 3, 4)
12
iex(3)> Geom.area(:pentagon, 3, 4)
0
iex(4)> Geom.area(:ellipse, -1, 5)
0
iex(5)> Geom.area(:triangle, 5, -1)
0

See a suggested solution in Appendix A.

Étude 3-4: Tuples as Parameters

Add an area/1 function that takes a tuple of the form {shape,number,number} as its parameter. The area/1 function will call the private area/3 function. Hint: use defp for private functions.

Here is some sample output. The last line tests to see that you cannot call the private function directly.

iex(1)> c("geom.ex")
[Geom]
iex(2)> Geom.area({:rectangle, 7, 3})
21
iex(3)> Geom.area({:triangle, 7, 3})
10.5
iex(4)> Geom.area({:ellipse, 7, 3})
65.97344572538566
iex(5)> Geom.area({:pentagon, 7, 3})
0
iex(6)> Geom.area(:rectangle, 7, 3)
** (UndefinedFunctionError) undefined function: Geom.area/3
    Geom.area(:rectangle, 7, 3)
    erl_eval.erl:569: :erl_eval.do_apply/6
    src/elixir.erl:133: :elixir.eval_forms/3
    /bin/elixir/lib/iex/lib/iex/server.ex:19: IEx.Server.do_loop/1

See a suggested solution in Appendix A.