Avatar of erszcz

erszcz's solution

to Triangle in the PureScript Track

Published at Jul 13 2018 · 2 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.## 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 Control.Monad.Eff (Eff)
import Control.Monad.Aff.AVar (AVAR)
import Control.Monad.Eff.Console (CONSOLE)
import Data.Either (Either(Left, Right))
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Console (TESTOUTPUT)
import Test.Unit.Main (runTest)
import Triangle (triangleKind, Triangle(Equilateral, Isosceles, Scalene))

main :: forall eff
  . Eff ( avar :: AVAR
        , console :: CONSOLE
        , testOutput :: TESTOUTPUT
        | eff                     
        )
        Unit
main = runTest suites

suites :: forall e. TestSuite e
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

instance eqTriangle :: Eq Triangle where
  eq Equilateral Equilateral = true
  eq Isosceles Isosceles = true
  eq Scalene Scalene = true
  eq _ _ = false

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 | isEquilateral a b c = Right Equilateral
triangleKind a b c | isIsosceles a b c = Right Isosceles
triangleKind a b c | isTriangle a b c = Right Scalene
triangleKind a b c | allSidesPositive a b c = Left "Violates inequality"
triangleKind _ _ _ = Left "Invalid lengths"

isEquilateral a b c | a == b && b == c = isTriangle a b c
isEquilateral _ _ _                    = false

isIsosceles a b c | a == b || b == c || a == c = isTriangle a b c
isIsosceles _ _ _                              = false

isTriangle a b c | allSidesPositive a b c && triangleInequality a b c = true
isTriangle _ _ _ = false

allSidesPositive a b c | a > 0 && b > 0 && c > 0 = true
allSidesPositive _ _ _                           = false

triangleInequality a b c | a + b >= c && a + c >= b && b + c >= a = true
triangleInequality _ _ _                                          = false

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of icyrockcom

You might want to remove some unnecessary pattern matches, e.g.: allSidesPositive a b c | a > 0 && b > 0 && c > 0 = true allSidesPositive _ _ _ = false

can be shortened to: allSidesPositive a b c = a > 0 && b > 0 && c > 0

Avatar of LeaveNhA

Wow! Such a hard work.

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?
  • Are there new concepts here that you could read more about to improve your understanding?