ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

bkhl's solution

to Triangle in the Prolog Track

Published at Sep 26 2019 · 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.## 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.plt

``````pending :-
current_prolog_flag(argv, ['--all'|_]).
pending :-
write('\nA TEST IS PENDING!\n'),
fail.

:- begin_tests(triangle).

test(equilateral_triangle_has_all_sides_equal, condition(true)) :-
triangle(2, 2, 2, Result),
Result == "equilateral".

test(larger_equilateral_triangle, condition(pending)) :-
triangle(10, 10, 10, "equilateral").

test(isosceles_triangle_with_last_two_sides_equal, condition(pending)) :-
triangle(3, 4, 4, "isosceles").

test(isosceles_triangle_with_first_two_sides_equal, condition(pending)) :-
triangle(4, 4, 3, "isosceles").

test(isosceles_triangle_with_first_and_last_sides_equal, condition(pending)) :-
triangle(4, 3, 4, "isosceles").

test(isosceles_triangle_with_unequal_side_larger_than_equal_sides, condition(pending)) :-
triangle(4, 7, 4, "isosceles").

test(scalene_triangle_has_no_equal_sides, condition(pending)) :-
triangle(3, 4, 5, "scalene").

test(a2_equals_b_plus_c_isnt_always_equilateral, condition(pending)) :-
triangle(5, 4, 6, "scalene").

test(larger_scalene_triangle, condition(pending)) :-
triangle(10, 11, 12, "scalene").

test(scalene_triangle_with_sides_in_descending_order, condition(pending)) :-
triangle(5, 4, 2, "scalene").

test(small_scalene_triangle_with_floating_point_values, condition(pending)) :-
triangle((0.4), (0.6), (0.3), "scalene").

test(a_triangle_violating_the_triangle_inequality_is_illegal, [fail, condition(pending)]) :-
triangle(7, 3, 2, _).

test(two_sides_equal_but_violates_triangle_inequality, [fail, condition(pending)]) :-
triangle(1, 1, 3, _).

test(triangles_with_all_sides_zero_are_illegal, [fail, condition(pending)]) :-
triangle(0, 0, 0, _).

:- end_tests(triangle).``````
``````triangle(X, Y, Z, T) :-
msort([X, Y, Z], [A, B, C]),
A > 0,
A + B >= C,
sorted_triangle(A, B, C, T).

sorted_triangle(X, X, X, "equilateral") :- !.

sorted_triangle(X, X, _, "isosceles") :- !.
sorted_triangle(_, X, X, "isosceles") :- !.

sorted_triangle(X, Y, Z, "scalene") :- X =\= Y, X =\= Z, !.``````