ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

hyphenrf's solution

to Space Age in the OCaml Track

Published at May 02 2020 · 0 comments
Instructions
Test suite
Solution

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

1. 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:

``````make
``````

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!

Source

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

test.ml

``````(* space-age - 1.2.0 *)
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.05 31.69 (age_on Earth 1000000000);
"age on Mercury" >::
ae ~delta:0.05 280.88 (age_on Mercury 2134835688);
"age on Venus" >::
ae ~delta:0.05 9.78 (age_on Venus 189839836);
"age on Mars" >::
ae ~delta:0.05 35.88 (age_on Mars 2129871239);
"age on Jupiter" >::
ae ~delta:0.05 2.41 (age_on Jupiter 901876382);
"age on Saturn" >::
ae ~delta:0.05 2.15 (age_on Saturn 2000000000);
"age on Uranus" >::
ae ~delta:0.05 0.46 (age_on Uranus 1210123456);
"age on Neptune" >::
ae ~delta:0.05 0.35 (age_on Neptune 1821023456);
]

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

let age_on spacerock secs =
let ey = float secs /. 31557600. in
match spacerock with
| Earth   -> ey           | Jupiter -> ey/.11.862615
| Mercury -> ey/.0.240846 | Saturn  -> ey/.29.447498
| Venus   -> ey/.0.615197 | Neptune -> ey/.164.79132
| Mars    -> ey/.1.880815 | Uranus  -> ey/.84.016846``````