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

aimorris's solution

to Triangle in the PureScript Track

Published at Jan 14 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 Data.Either (Either (..))
import Data.Set as Set
import Prelude
import Data.Array as Array

data Triangle = Equilateral
| Isosceles
| Scalene
derive instance eqTriangle :: Eq Triangle

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

findKind :: Array Int -> Either String Triangle
findKind x = case Array.length x of
1 -> Right Equilateral
2 -> Right Isosceles
3 -> Right Scalene
_ -> Left "Not a triangle"

triangleKind :: Int -> Int -> Int -> Either String Triangle
triangleKind s1 s2 s3
| s1 < 1 || s2 < 1 || s3 < 1 = Left "Invalid lengths"
| s1 + s2 < s3 || s2 + s3 < s1 || s3 + s1 < s2 = Left "Violates inequality"
| otherwise = findKind \$ Set.toUnfoldable \$ Set.fromFoldable [s1, s2, s3]``````