Avatar of paulfioravanti

paulfioravanti's solution

to Leap in the Elm Track

Published at Jun 20 2019 · 0 comments
Test suite

Given a year, report if it is a leap year.

The tricky thing here is that a leap year in the Gregorian calendar occurs:

on every year that is evenly divisible by 4
  except every year that is evenly divisible by 100
    unless the year is also evenly divisible by 400

For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap year, but 2000 is.


Though our exercise adopts some very simple rules, there is more to learn!

For a delightful, four minute explanation of the whole leap year phenomenon, go watch this youtube video.

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

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!


JavaRanch Cattle Drive, exercise 3 http://www.javaranch.com/leap.jsp

Submitting Incomplete Solutions

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


module Tests exposing (tests)

import Expect
import Leap
import Test exposing (..)

tests : Test
tests =
    describe "Leap"
        [ test "leap year" <|
            \() -> Expect.equal True (Leap.isLeapYear 1996)
        , skip <|
            test "non-leap year" <|
                \() -> Expect.equal False (Leap.isLeapYear 1997)
        , skip <|
            test "non-leap even year" <|
                \() -> Expect.equal False (Leap.isLeapYear 1998)
        , skip <|
            test "century" <|
                \() -> Expect.equal False (Leap.isLeapYear 1900)
        , skip <|
            test "second century" <|
                \() -> Expect.equal False (Leap.isLeapYear 1800)
        , skip <|
            test "fourth century" <|
                \() -> Expect.equal True (Leap.isLeapYear 2400)
        , skip <|
            test "y2k" <|
                \() -> Expect.equal True (Leap.isLeapYear 2000)
module Leap exposing (isLeapYear)

isLeapYear : Int -> Bool
isLeapYear year =
    isLeap year && (isNonCenturialYear year || isLeapCycleYear year)


isLeap : Int -> Bool
isLeap year =
    modBy leapYear year == 0

isNonCenturialYear : Int -> Bool
isNonCenturialYear year =
    not (modBy centurialYear year == 0)

isLeapCycleYear : Int -> Bool
isLeapCycleYear year =
        leapCycleLength =
            leapYear * centurialYear
    modBy leapCycleLength year == 0

leapYear : Int
leapYear =

centurialYear : Int
centurialYear =

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?