É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.
1>
c
(
geom
).
{ok,geom}
2>
geom
:
area
(
rectangle
,
3
,
4
).
12
3>
geom
:
area
(
triangle
,
3
,
5
).
7.5
4>
geom
:
area
(
ellipse
,
2
,
4
).
25.132741228718345
Get Études for Erlang now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.