Avatar of shmibs

shmibs's solution

to Space Age in the OCaml Track

Published at Dec 08 2018 · 0 comments
Test suite


This exercise has changed since this solution was written.

Given an age in seconds, calculate how old someone would be on:

  • Earth: orbital period 365.25 Earth days, or 31557600 seconds
  • Mercury: orbital period 0.2408467 Earth years
  • Venus: orbital period 0.61519726 Earth years
  • Mars: orbital period 1.8808158 Earth years
  • Jupiter: orbital period 11.862615 Earth years
  • Saturn: orbital period 29.447498 Earth years
  • Uranus: orbital period 84.016846 Earth years
  • Neptune: orbital period 164.79132 Earth years

So if you were told someone were 1,000,000,000 seconds old, you should be able to say that they're 31.69 Earth-years old.

If you're wondering why Pluto didn't make the cut, go watch this youtube video.

Getting Started

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


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:


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!


Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=01

Submitting Incomplete Solutions

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


open Base
open OUnit2
open Space_age

let ae ~delta:delta exp got _ctxt =
  let msg = Printf.sprintf "Expected %f got %f, difference is greater than %f"
                    exp got delta in
  assert_bool msg (cmp_float ~epsilon:delta exp got)

let tests = [
   "age on Earth" >::
      ae ~delta:0.005 31.69 (age_on Earth 1000000000);
   "age on Mercury" >::
      ae ~delta:0.005 280.88 (age_on Mercury 2134835688);
   "age on Venus" >::
      ae ~delta:0.005 9.78 (age_on Venus 189839836);
   "age on Mars" >::
      ae ~delta:0.005 39.25 (age_on Mars 2329871239);
   "age on Jupiter" >::
      ae ~delta:0.005 2.41 (age_on Jupiter 901876382);
   "age on Saturn" >::
      ae ~delta:0.005 3.23 (age_on Saturn 3000000000);
   "age on Uranus" >::
      ae ~delta:0.005 1.21 (age_on Uranus 3210123456);
   "age on Neptune" >::
      ae ~delta:0.005 1.58 (age_on Neptune 8210123456);

let () =
  run_test_tt_main ("space-age tests" >::: tests)
type planet = Mercury | Venus  | Earth   | Mars
            | Jupiter | Saturn | Neptune | Uranus

let earth_year_len = 31557600.

let year_len_mult = function
  | Mercury -> 0.2408467
  |   Venus -> 0.6151972
  |   Earth -> 1.0000000
  |    Mars -> 1.8808158
  | Jupiter -> 11.862615
  |  Saturn -> 29.447498
  | Neptune -> 164.79132
  |  Uranus -> 84.016846

let age_on (plnt : planet) (sec : int) =
  year_len_mult plnt
  |> ( *.) earth_year_len
  |> ( /.) (Float.of_int sec)

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?