Avatar of shmibs

shmibs's solution

to Triangle in the OCaml Track

Published at Dec 03 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.

Installation

To work on the exercises, you will need Opam and Base. Consult opam website for instructions on how to install opam for your OS. Once opam is installed open a terminal window and run the following command to install base:

opam install base

To run the tests you will need OUnit. Install it using opam:

opam install ounit

Running Tests

A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:

make

Interactive Shell

utop is a command line program which allows you to run Ocaml code interactively. The easiest way to install it is via opam:

opam install utop

Consult utop for more detail.

Feedback, Issues, Pull Requests

The exercism/ocaml repository on GitHub is the home for all of the Ocaml 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.

test.ml

open Base
open OUnit2
open Triangle

let ae exp got _test_ctxt = assert_equal exp got ~printer:Bool.to_string

let equilateral_tests = [
   "true if all sides are equal" >::
     ae true (is_equilateral 2 2 2);
   "false if any side is unequal" >::
     ae false (is_equilateral 2 3 2);
   "false if no sides are equal" >::
     ae false (is_equilateral 5 4 6);
   "All zero sides are illegal, so the triangle is not equilateral" >::
     ae false (is_equilateral 0 0 0);
]

let isosceles_tests = [
   "true if last two sides are equal" >::
     ae true (is_isosceles 3 4 4);
   "true if first two sides are equal" >::
     ae true (is_isosceles 4 4 3);
   "true if first and last sides are equal" >::
     ae true (is_isosceles 4 3 4);
   "equilateral triangles are also isosceles" >::
     ae true (is_isosceles 4 4 4);
   "false if no sides are equal" >::
     ae false (is_isosceles 2 3 4);
   "Sides that violate triangle inequality are not isosceles, even if two are equal (1)" >::
     ae false (is_isosceles 1 1 3);
   "Sides that violate triangle inequality are not isosceles, even if two are equal (2)" >::
     ae false (is_isosceles 1 3 1);
   "Sides that violate triangle inequality are not isosceles, even if two are equal (3)" >::
     ae false (is_isosceles 3 1 1);
]

let scalene_tests = [
   "true if no sides are equal" >::
     ae true (is_scalene 5 4 6);
   "false if all sides are equal" >::
     ae false (is_scalene 4 4 4);
   "false if two sides are equal" >::
     ae false (is_scalene 4 4 3);
   "Sides that violate triangle inequality are not scalene, even if they are all different" >::
     ae false (is_scalene 7 3 2);
]

let () =
  run_test_tt_main (
    "triangle tests" >:::
      List.concat [equilateral_tests; isosceles_tests; scalene_tests]
  )
open Base

let side_check (a : int) (b : int) (c : int) =
  a > 0 && b > 0 && c > 0
  && a + b >= c && b + c >= a && c + a >= b


let is_equilateral (a : int) (b : int) (c : int) =
  a = b && b = c && a > 0


let is_isosceles (a : int) (b : int) (c : int) =
  (a = b || b = c || c = a) && (side_check a b c)


let is_scalene (a : int) (b : int) (c : int) =
  a <> b && b <> c && (side_check a b c)

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?