exercism/elixir/triangle/lib/triangle.ex

14 lines
639 B
Elixir
Raw Normal View History

2024-06-29 20:34:53 +00:00
defmodule Triangle do
@type kind :: :equilateral | :isosceles | :scalene
@doc """
Return the kind of triangle of a triangle with 'a', 'b' and 'c' as lengths.
"""
@spec kind(number, number, number) :: {:ok, kind} | {:error, String.t()}
def kind(a, b, c) when a <= 0 or b <= 0 or c <= 0, do: {:error, "all side lengths must be positive"}
def kind(a, b, c) when a + b <= c or a + c <= b or b + c <= a, do: {:error, "side lengths violate triangle inequality"}
def kind(a, a, a), do: {:ok, :equilateral}
def kind(a, b, c) when a == b or b == c or c == a, do: {:ok, :isosceles}
def kind(_a, _b, _c), do: {:ok, :scalene}
end