# rootulp's solution

## to Triangle in the Elm Track

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

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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.

## Elm Installation

Refer to the Exercism help page for Elm installation and learning resources.

## Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed.

``````\$ elm-package install --yes
``````

Execute the tests with:

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

Automatically run tests again when you save changes:

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

As you work your way through the test suite, 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's possible to submit an incomplete solution so you can see how others have completed the exercise.

### Tests.elm

``````module Tests exposing (..)

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

tests : Test
tests =
describe "triangleKind"
[ test "the solution is for the correct version of the test" <|
\() -> Expect.equal 2 version
, 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)
]``````

### elm-package.json

``````{
"version": "3.0.0",
"summary": "Exercism problems in Elm.",
"repository": "https://github.com/exercism/elm.git",
"source-directories": [
".",
".."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"elm-community/elm-test": "4.0.0 <= v < 5.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}``````
``````module Triangle exposing (..)

type Triangle = Equilateral | Isosceles | Scalene

triangleKind: comparable -> comparable -> comparable -> Result String Triangle
triangleKind a b c =
if invalidLengths a b c then
(Err "Invalid lengths")
else if violatesInequality a b c then
(Err "Violates inequality")
else if isEquilateral a b c then
(Ok Equilateral)
else if isIsosceles a b c then
(Ok Isosceles)
else
(Ok Scalene)

isEquilateral: comparable -> comparable -> comparable -> Bool
isEquilateral a b c =
a == b  && b == c

isIsosceles: comparable -> comparable -> comparable -> Bool
isIsosceles a b c =
(a == b && b /= c) ||
(a == c && c /= b) ||
(b == c && c /= a)

invalidLengths: comparable -> comparable -> comparable -> Bool
invalidLengths a b c =
a <= 0 || b <= 0 || c <= 0

violatesInequality: comparable -> comparable -> comparable -> Bool
violatesInequality a b c =
(a + b <= c) ||
(a + c <= b) ||
(b + c <= a)

version : Int
version =
2``````