Given an age in seconds, calculate how old someone would be on:
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.
Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
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!
Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=01
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)
open Base;; type planet = Mercury | Venus | Earth | Mars | Jupiter | Saturn | Neptune | Uranus ;; let planet_orbital_period planet = let earth_year = 31557600.0 in match planet with | Mercury -> earth_year *. 0.2408467 | Venus -> earth_year *. 0.61519726 | Earth -> earth_year | Mars -> earth_year *. 1.8808158 | Jupiter -> earth_year *. 11.862615 | Saturn -> earth_year *. 29.447498 | Uranus -> earth_year *. 84.016846 | Neptune -> earth_year *. 164.79132 let age_on planet seconds = let orbital_period = planet_orbital_period planet in Float.of_int seconds /. orbital_period ;;
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.