# 4d47's solution

## to Triangle in the Erlang Track

Published at Jul 22 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Determine if a triangle is equilateral, isosceles, or scalene.

An equilateral triangle has all three sides the same length.

An isosceles triangle has at least two sides the same length. (It is sometimes specified as having exactly two sides the same length, but for the purposes of this exercise we'll say at least two.)

A scalene triangle has all sides of different lengths.

## Note

For a shape to be a triangle at all, all sides have to be of length > 0, and the sum of the lengths of any two sides must be greater than or equal to the length of the third side. See Triangle Inequality.

## Dig Deeper

The case where the sum of the lengths of two sides equals that of the third is known as a degenerate triangle - it has zero area and looks like a single line. Feel free to add your own code/tests to check for degenerate triangles.

## Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, `rebar3` is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

``````\$ rebar3 eunit
``````

### Test versioning

Each problem defines a macro `TEST_VERSION` in the test file and verifies that the solution defines and exports a function `test_version` returning that same value.

To make tests pass, add the following to your solution:

``````-export([test_version/0]).

test_version() ->
1.
``````

The benefit of this is that reviewers can see against which test version an iteration was written if, for example, a previously posted solution does not solve the current problem or passes current tests.

## Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Source

The Ruby Koans triangle project, parts 1 & 2 http://rubykoans.com

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### triangle_tests.erl

``````-module(triangle_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

equilateral_triangles_have_equal_sides_test() ->
?assertMatch(equilateral, triangle:kind(2,2,2)).

larger_equilateral_triangles_also_have_equal_sides_test() ->
?assertMatch(equilateral, triangle:kind(10, 10, 10)).

isosceles_triangles_have_at_least_two_sides_equal_test() ->
?assertMatch(isosceles, triangle:kind(3, 4, 4)).

isosceles_triangles_have_first_and_last_sides_equal_test() ->
?assertMatch(isosceles, triangle:kind(4, 3, 4)).

isosceles_triangles_have_two_first_sides_equal_test() ->
?assertMatch(isosceles, triangle:kind(4, 4, 3)).

isosceles_triangles_have_in_fact_exactly_two_sides_equal_test() ->
?assertMatch(isosceles, triangle:kind(10, 10, 2)).

scalene_triangles_have_no_equal_sides_test() ->
?assertMatch(scalene, triangle:kind(3, 4, 5)).

scalene_triangles_have_no_equal_sides_at_a_larger_scale_too_test() ->
?assertMatch(scalene, triangle:kind(10, 11, 12)).

scalene_triangles_have_no_equal_sides_in_descending_order_either_test() ->
?assertMatch(scalene, triangle:kind(5, 4, 2)).

very_small_triangles_are_legal_test() ->
?assertMatch(scalene, triangle:kind(0.4, 0.6, 0.3)).

triangles_with_no_size_are_illegal_test() ->
?assertMatch({error, "all side lengths must be positive"},
triangle:kind(0, 0, 0)).

triangles_with_negative_sides_are_illegal_test() ->
?assertMatch({error, "all side lengths must be positive"},
triangle:kind(3, 4, -5)).

triangles_violating_triangle_inequality_are_illegal_test() ->
?assertMatch({error, "side lengths violate triangle inequality"},
triangle:kind(1, 1, 3)).

triangles_violating_triangle_inequality_are_illegal_2_test() ->
?assertMatch({error, "side lengths violate triangle inequality"},
triangle:kind(2, 4, 2)).

triangles_violating_triangle_inequality_are_illegal_3_test() ->
?assertMatch({error, "side lengths violate triangle inequality"},
triangle:kind(7, 3, 2)).

version_test() ->
?assertMatch(1, triangle:test_version()).``````
``````-module(triangle).
-export([kind/3, test_version/0]).

test_version() ->
1.

kind(A, B, C) when A =< 0; B =< 0; C =< 0 ->
{error, "all side lengths must be positive"};
kind(A, B, C) when A + B =< C; A + C =< B; B + C =< A ->
{error, "side lengths violate triangle inequality"};
kind(A, B, C) when A == B, A == C ->
equilateral;
kind(A, B, C) when A == B; A == C; B == C ->
isosceles;
kind(_, _, _) ->
scalene.``````

## Community comments

Find this solution interesting? Ask the author a question to learn more.

### What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?
• Are there new concepts here that you could read more about to improve your understanding?