Avatar of paulfioravanti

paulfioravanti's solution

to Triangle in the Elm Track

Published at Jun 24 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.

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

$ elm-test

Automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the test suite, be sure to remove the skip <| calls from each test until you get them all passing!

Source

The Ruby Koans triangle project, parts 1 & 2 http://rubykoans.com

Submitting Incomplete Solutions

It is possible to submit an incomplete solution so you can see how others have completed the exercise.

Tests.elm

module Tests exposing (tests)

import Expect
import Test exposing (..)
import Triangle exposing (Triangle(..), triangleKind)


tests : Test
tests =
    describe "triangleKind"
        [ test "equilateral triangles have equal sides" <|
            \() -> Expect.equal (Ok Equilateral) (triangleKind 2 2 2)
        , skip <|
            test "larger equilateral triangles also have equal sides" <|
                \() -> Expect.equal (Ok Equilateral) (triangleKind 10 10 10)
        , skip <|
            test "isosceles triangles have last two sides equal" <|
                \() -> Expect.equal (Ok Isosceles) (triangleKind 3 4 4)
        , skip <|
            test "isosceles triangles have first and last sides equal" <|
                \() -> Expect.equal (Ok Isosceles) (triangleKind 4 3 4)
        , skip <|
            test "isosceles triangles have two first sides equal" <|
                \() -> Expect.equal (Ok Isosceles) (triangleKind 4 4 3)
        , skip <|
            test "isosceles triangles have in fact exactly two sides equal" <|
                \() -> Expect.equal (Ok Isosceles) (triangleKind 10 10 2)
        , skip <|
            test "scalene triangles have no equal sides" <|
                \() -> Expect.equal (Ok Scalene) (triangleKind 3 4 5)
        , skip <|
            test "scalene triangles have no equal sides at a larger scale too" <|
                \() -> Expect.equal (Ok Scalene) (triangleKind 10 11 12)
        , skip <|
            test "scalene triangles have no equal sides at a larger scale too 2" <|
                \() -> Expect.equal (Ok Scalene) (triangleKind 5 4 2)
        , skip <|
            test "very small triangles are legal" <|
                \() -> Expect.equal (Ok Scalene) (triangleKind 0.4 0.6 0.3)
        , skip <|
            test "triangles with no size are illegal" <|
                \() -> Expect.equal (Err "Invalid lengths") (triangleKind 0 0 0)
        , skip <|
            test "triangles with negative sides are illegal" <|
                \() -> Expect.equal (Err "Invalid lengths") (triangleKind 3 4 -5)
        , skip <|
            test "triangles violating triangle inequality are illegal 1" <|
                \() -> Expect.equal (Err "Violates inequality") (triangleKind 1 1 3)
        , skip <|
            test "triangles violating triangle inequality are illegal 2" <|
                \() -> Expect.equal (Err "Violates inequality") (triangleKind 7 3 2)
        ]
module Triangle exposing (Triangle(..), triangleKind)

import Set


type Triangle
    = Equilateral
    | Isosceles
    | Scalene


triangleKind : number -> number -> number -> Result String Triangle
triangleKind x y z =
    let
        sides =
            List.sort [ x, y, z ]

        validSides =
            allSidesPositive sides

        validInequality =
            triangleInequality sides
    in
    if not validSides then
        Err "Invalid lengths"

    else if not validInequality then
        Err "Violates inequality"

    else
        sides
            |> Set.fromList
            |> Set.size
            |> determineTriangleType



-- PRIVATE


allSidesPositive : List number -> Bool
allSidesPositive sides =
    let
        isPositive num =
            num > 0
    in
    List.all isPositive sides


triangleInequality : List number -> Bool
triangleInequality sides =
    let
        ( head, tail ) =
            case List.reverse sides of
                h :: t ->
                    ( h, t )

                [] ->
                    ( 0, [] )
    in
    List.sum tail > head


determineTriangleType : Int -> Result String Triangle
determineTriangleType numUniqueSides =
    case numUniqueSides of
        1 ->
            Ok Equilateral

        2 ->
            Ok Isosceles

        3 ->
            Ok Scalene

        _ ->
            Err "Not a triangle"

Community comments

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

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?