# atmantree's solution

## to Triangle in the Haskell Track

Published at Sep 02 2018 · 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.

## Getting Started

For installation and learning resources, refer to the exercism help page.

## Running the tests

To run the test suite, execute the following command:

``````stack test
``````

#### If you get an error message like this...

``````No .cabal file found in directory
``````

You are probably running an old stack version and need to upgrade it.

#### Otherwise, if you get an error message like this...

``````No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...
``````

Just do as it says and it will download and install the correct compiler version:

``````stack setup
``````

## Running GHCi

If you want to play with your solution in GHCi, just run the command:

``````stack ghci
``````

## Feedback, Issues, Pull Requests

The exercism/haskell repository on GitHub is the home for all of the Haskell exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!

## 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.hs

``````{-# OPTIONS_GHC -fno-warn-type-defaults #-}

import Data.Foldable     (for_)
import Test.Hspec        (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import Triangle
( TriangleType ( Equilateral
, Illegal
, Isosceles
, Scalene
)
, triangleType
)

main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = describe "triangleType" \$ for_ cases test
where

test (description, (a, b, c), expected) = it description assertion
where
assertion = triangleType a b c `shouldBe` expected

cases = [ ( "equilateral triangle has all sides equal"
, (2, 2, 2)
, Equilateral
)
, ( "larger equilateral triangle"
, (10, 10, 10)
, Equilateral
)
, ( "isosceles triangle with last two sides equal"
, (3, 4, 4)
, Isosceles
)
, ( "isosceles triangle with first two sides equal"
, (4, 4, 3)
, Isosceles
)
, ( "isosceles triangle with first and last sides equal"
, (4, 3, 4)
, Isosceles
)
, ( "isosceles triangle with unequal side larger than equal sides"
, (4, 7, 4)
, Isosceles
)
, ( "scalene triangle has no equal sides"
, (3, 4, 5)
, Scalene
)
, ( "2a == b+c looks like equilateral, but isn't always"
, (5, 4, 6)
, Scalene
)
, ( "larger scalene triangle"
, (10, 11, 12)
, Scalene
)
, ( "scalene triangle with sides in descending order"
, (5, 4, 2)
, Scalene
)
, ( "small scalene triangle with floating point values"
, (0.4, 0.6, 0.3)
, Scalene
)
, ( "a triangle violating the triangle inequality is illegal"
, (7, 3, 2)
, Illegal
)
, ( "two sides equal, but still violates triangle inequality"
, (1, 1, 3)
, Illegal
)
, ( "triangles with all sides zero are illegal"
, (0, 0, 0)
, Illegal
)
]``````
``````module Triangle (TriangleType(..), triangleType) where

import Data.List

data TriangleType = Equilateral
| Isosceles
| Scalene
| Illegal
deriving (Eq, Show)

isLegal :: (Ord a, Num a) => a -> a -> a -> Bool
isLegal a b c = maximum [a, b, c] < a + b + c - maximum [a, b, c]

triangleType :: (Ord a, Eq a, Num a) => a -> a -> a -> TriangleType
triangleType a b c
| a == 0 || b == 0 || c == 0 || not (isLegal a b c)  = Illegal
| a == b && b == c                                   = Equilateral
| a == b || a == c || b == c                         = Isosceles
| otherwise                                          = Scalene``````