Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Space Age in the Elm Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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.

Elm Installation

Refer to the Exercism help page for Elm installation and learning resources.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed.

$ elm-package install --yes

Execute the tests with:

$ elm-test

Automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the test suite, be sure to remove the skip <| calls from each test until you get them all passing!

Source

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.

Tests.elm

module Tests exposing (..)

import Expect
import SpaceAge exposing (Planet(..), ageOn)
import Test exposing (..)


tests : Test
tests =
    describe "SpaceAge"
        [ test "age in earth years" <|
            \() -> Expect.equal 32 (round (ageOn Earth 1000000000.0))
        , skip <|
            test "age in mercury years" <|
                \() -> Expect.equal 281 (round (ageOn Mercury 2134835688.0))
        , skip <|
            test "age in venus years" <|
                \() -> Expect.equal 10 (round (ageOn Venus 189839836.0))
        , skip <|
            test "age on mars" <|
                \() -> Expect.equal 39 (round (ageOn Mars 2329871239.0))
        , skip <|
            test "age on jupiter" <|
                \() -> Expect.equal 2 (round (ageOn Jupiter 901876382.0))
        , skip <|
            test "age on saturn" <|
                \() -> Expect.equal 3 (round (ageOn Saturn 3000000000.0))
        , skip <|
            test "age on uranus" <|
                \() -> Expect.equal 1 (round (ageOn Uranus 3210123456.0))
        , skip <|
            test "age on neptune" <|
                \() -> Expect.equal 2 (round (ageOn Neptune 8210123456.0))
        ]

elm-package.json

{
    "version": "3.0.0",
    "summary": "Exercism problems in Elm.",
    "repository": "https://github.com/exercism/elm.git",
    "license": "BSD3",
    "source-directories": [
        ".",
        ".."
    ],
    "exposed-modules": [],
    "dependencies": {
        "elm-lang/core": "5.0.0 <= v < 6.0.0",
        "elm-community/elm-test": "4.0.0 <= v < 5.0.0"
    },
    "elm-version": "0.18.0 <= v < 0.19.0"
}
module SpaceAge exposing (..)

type Planet
  = Mercury
  | Venus
  | Earth
  | Mars
  | Jupiter
  | Saturn
  | Uranus
  | Neptune

ageOn: Planet -> Float -> Float
ageOn planet ageInSeconds =
  ageInSeconds / orbitalPeriod planet

orbitalPeriod: Planet -> Float
orbitalPeriod planet =
  case planet of
    Mercury -> 7600530.24
    Venus -> 19413907.2
    Earth -> 31558149.76
    Mars -> 59354294.4
    Jupiter -> 374335776.0
    Saturn -> 929596608.0
    Uranus -> 2661041808.0
    Neptune -> 5200418592.0

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?