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

# lifchicker's solution

## to Triangle in the PureScript Track

Published at Aug 07 2020 · 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.Array (nub)
import Data.Either (Either(Right, Left))
import Data.Foldable (all, any)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import Data.Tuple (Tuple(..))

data Triangle
= Equilateral
| Isosceles
| Scalene

derive instance eqTriangle :: Eq Triangle
derive instance genericTriangle :: Generic Triangle _

instance showTriangle :: Show Triangle where
show = genericShow

pairs :: Int -> Int -> Int -> Array (Tuple Int Int)
pairs x y z = [Tuple x y, Tuple x z, Tuple y z]

correctLength :: Int -> Int -> Int -> Boolean
correctLength x y z = all (_ > 0) [x, y, z]

rightTriangle :: Int -> Int -> Int -> Boolean
rightTriangle x y z = satisfy x y z && satisfy y z x && satisfy z x y
where satisfy a b c = a + b >= c

isEquilateral :: Int -> Int -> Int -> Boolean
isEquilateral x y z = x == y && y == z

isIsosceles :: Int -> Int -> Int -> Boolean
isIsosceles x y z = any (\(Tuple l r) -> l == r) (nub \$ pairs x y z)

triangleKind :: Int -> Int -> Int -> Either String Triangle
triangleKind x y z
| not \$ correctLength x y z = Left "Invalid lengths"
| not \$ rightTriangle x y z = Left "Violates inequality"
| isEquilateral x y z = Right Equilateral
| isIsosceles x y z = Right Isosceles
| otherwise = Right Scalene``````

### 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?