🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of hyphenrf

hyphenrf's solution

to Triangle in the OCaml Track

Published at May 02 2020 · 0 comments
Test suite

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.


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

  1. Install the Exercism CLI.

  2. Install OCaml.

  3. For library documentation, follow Useful OCaml resources.

Running Tests

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


Submitting Incomplete Solutions

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

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 or submit a PR. We welcome new contributors!


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


(* triangle - 1.2.1 *)
open Base
open OUnit2
open Triangle

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

let equilateral_triangle_tests = [
  "all sides are equal" >::
  ae true (is_equilateral 2 2 2);
  "any side is unequal" >::
  ae false (is_equilateral 2 3 2);
  "no sides are equal" >::
  ae false (is_equilateral 5 4 6);
  "all zero sides is not a triangle" >::
  ae false (is_equilateral 0 0 0);
let isosceles_triangle_tests = [
  "last two sides are equal" >::
  ae true (is_isosceles 3 4 4);
  "first two sides are equal" >::
  ae true (is_isosceles 4 4 3);
  "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);
  "no sides are equal" >::
  ae false (is_isosceles 2 3 4);
  "first triangle inequality violation" >::
  ae false (is_isosceles 1 1 3);
  "second triangle inequality violation" >::
  ae false (is_isosceles 1 3 1);
  "third triangle inequality violation" >::
  ae false (is_isosceles 3 1 1);
let scalene_triangle_tests = [
  "no sides are equal" >::
  ae true (is_scalene 5 4 6);
  "all sides are equal" >::
  ae false (is_scalene 4 4 4);
  "two sides are equal" >::
  ae false (is_scalene 4 4 3);
  "may not violate triangle inequality" >::
  ae false (is_scalene 7 3 2);

let () =
  run_test_tt_main (
    "triangle tests" >:::
    List.concat [
let validate a b c =
  a+b > c && b+c > a && c+a > b

let is_equilateral a b c = validate a b c
  && a = b && b = c

let is_isosceles a b c   = validate a b c
  && (a = b || b = c || c = a)

let is_scalene a b c     = validate a b c
  && a <> b && 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?