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
(* 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
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.