🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # epequeno's solution

## to Triangle in the Elm Track

Published at Apr 24 2021 · 0 comments
Instructions
Test suite
Solution

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.

## Elm Installation

Refer to the Installing Elm page for information about installing elm.

## Writing the Code

The code you have to write is located inside the `src/` directory of the exercise. Elm automatically installs packages dependencies the first time you run the tests so we can start by running the tests from the exercise directory with:

``````\$ elm-test
``````

To automatically run tests again when you save changes:

``````\$ elm-test --watch
``````

As you work your way through the tests suite in the file `tests/Tests.elm`, be sure to remove the `skip <|` calls from each test until you get them all passing!

## Source

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

## Submitting Incomplete Solutions

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

### Tests.elm

``````module Tests exposing (tests)

import Expect
import Test exposing (..)
import Triangle exposing (Triangle(..), triangleKind)

tests : Test
tests =
describe "triangleKind"
[ test "equilateral triangles have equal sides" <|
\() -> Expect.equal (Ok Equilateral) (triangleKind 2 2 2)
, skip <|
test "larger equilateral triangles also have equal sides" <|
\() -> Expect.equal (Ok Equilateral) (triangleKind 10 10 10)
, skip <|
test "isosceles triangles have last two sides equal" <|
\() -> Expect.equal (Ok Isosceles) (triangleKind 3 4 4)
, skip <|
test "isosceles triangles have first and last sides equal" <|
\() -> Expect.equal (Ok Isosceles) (triangleKind 4 3 4)
, skip <|
test "isosceles triangles have two first sides equal" <|
\() -> Expect.equal (Ok Isosceles) (triangleKind 4 4 3)
, skip <|
test "isosceles triangles have in fact exactly two sides equal" <|
\() -> Expect.equal (Ok Isosceles) (triangleKind 10 10 2)
, skip <|
test "scalene triangles have no equal sides" <|
\() -> Expect.equal (Ok Scalene) (triangleKind 3 4 5)
, skip <|
test "scalene triangles have no equal sides at a larger scale too" <|
\() -> Expect.equal (Ok Scalene) (triangleKind 10 11 12)
, skip <|
test "scalene triangles have no equal sides at a larger scale too 2" <|
\() -> Expect.equal (Ok Scalene) (triangleKind 5 4 2)
, skip <|
test "very small triangles are legal" <|
\() -> Expect.equal (Ok Scalene) (triangleKind 0.4 0.6 0.3)
, skip <|
test "triangles with no size are illegal" <|
\() -> Expect.equal (Err "Invalid lengths") (triangleKind 0 0 0)
, skip <|
test "triangles with negative sides are illegal" <|
\() -> Expect.equal (Err "Invalid lengths") (triangleKind 3 4 -5)
, skip <|
test "triangles violating triangle inequality are illegal 1" <|
\() -> Expect.equal (Err "Violates inequality") (triangleKind 1 1 3)
, skip <|
test "triangles violating triangle inequality are illegal 2" <|
\() -> Expect.equal (Err "Violates inequality") (triangleKind 7 3 2)
]``````
``````module Triangle exposing (Triangle(..), triangleKind)

import Set

type Triangle
= Equilateral
| Isosceles
| Scalene

triangleKind : number -> number -> number -> Result String Triangle
triangleKind x y z =
let
candidate =
[ x, y, z ] |> List.sort
in
if not (isValidLengths candidate) then
Err "Invalid lengths"

else if not (isValid candidate) then
Err "Violates inequality"

else if isEquilateral candidate then
Ok Equilateral

else if isIsosceles candidate then
Ok Isosceles

else if isScalene candidate then
Ok Scalene

else
Err "invalid input"

isValid : List number -> Bool
isValid sides =
case sides of
[ a, b, c ] ->
(a + b) >= c

_ ->
False

isValidLengths : List number -> Bool
isValidLengths sides =
not (isZeros sides) && not (anyNegative sides)

isZeros : List number -> Bool
isZeros =
List.sum
>> (==) 0

anyNegative : List number -> Bool
anyNegative =
List.any (\n -> n < 0)

setSize : List number -> Int
setSize =
Set.fromList
>> Set.size

isEquilateral : List number -> Bool
isEquilateral =
setSize
>> (==) 1

isIsosceles : List number -> Bool
isIsosceles =
setSize
>> (>=) 2

isScalene : List number -> Bool
isScalene =
setSize
>> (==) 3``````