 # mbraak's solution

## to Triangle in the PureScript Track

Published at Jan 31 2019 · 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.## 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.

### Main.purs

``````module Test.Main where

import Prelude

import Test.Unit.Assert as Assert
import Effect (Effect)
import Data.Either (Either(..))
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Main (runTest)
import Triangle (triangleKind, Triangle(Equilateral, Isosceles, Scalene))

main :: Effect Unit
main = runTest suites

suites :: TestSuite
suites = do
suite "Triangle.triangleKind" do
test "equilateral triangles have equal sides" do
Assert.equal
(Right Equilateral) \$
triangleKind 2 2 2
test "larger equilateral triangles also have equal sides" do
Assert.equal
(Right Equilateral)
\$ triangleKind 10 10 10
test "isosceles triangles have last two sides equal" do
Assert.equal
(Right Isosceles)
\$ triangleKind 3 4 4
test "isosceles triangles have first and last sides equal" do
Assert.equal
(Right Isosceles)
\$ triangleKind 4 3 4
test "isosceles triangles have two first sides equal" do
Assert.equal
(Right Isosceles)
\$ triangleKind 4 4 3
test "isosceles triangles have in fact exactly two sides equal" do
Assert.equal
(Right Isosceles)
\$ triangleKind 10 10 2
test "scalene triangles have no equal sides" do
Assert.equal
(Right Scalene)
\$ triangleKind 3 4 5
test "scalene triangles have no equal sides at a larger scale too" do
Assert.equal
(Right Scalene)
\$ triangleKind 10 11 12
test "scalene triangles have no equal sides at a larger scale too 2" do
Assert.equal
(Right Scalene)
\$ triangleKind 5 4 2
test "triangles with no size are illegal" do
Assert.equal
(Left "Invalid lengths")
\$ triangleKind 0 0 0
test "triangles with negative sides are illegal" do
Assert.equal
(Left "Invalid lengths")
\$ triangleKind 3 4 (-5)
test "triangles violating triangle inequality are illegal 1" do
Assert.equal
(Left "Violates inequality")
\$ triangleKind 1 1 3
test "triangles violating triangle inequality are illegal 2" do
Assert.equal
(Left "Violates inequality")
\$ triangleKind 7 3 2``````
``````module Triangle
( Triangle(Equilateral, Isosceles, Scalene)
, triangleKind
) where

import Prelude
import Data.Either (Either(..))

data Triangle = Equilateral | Isosceles | Scalene

derive instance eqTriangle :: Eq Triangle

instance showTriangle :: Show Triangle where
show Equilateral = "Equilateral"
show Isosceles = "Isosceles"
show Scalene = "Scalene"

triangleKind :: Int -> Int -> Int -> Either String Triangle

triangleKind a b c | a <= 0 || b <= 0 || c <= 0 =
Left "Invalid lengths"

triangleKind a b c | a + b <= c || b + c <= a || c + a <= b =
Left "Violates inequality"

triangleKind a b c | a == b && b == c =
Right Equilateral

triangleKind a b c | a == b || a == c || b == c =
Right Isosceles

triangleKind a b c | a /= b && b /= c =
Right Scalene

triangleKind a b c =
Left "Lengths must be positive"``````