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.
Refer to the Installing Elm page for information about installing elm.
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:
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
calls from each test until you get them all passing!
JavaRanch Cattle Drive, exercise 3 http://www.javaranch.com/leap.jsp
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 = let divisibleBy4 = remainderBy 4 year == 0 divisibleBy100 = remainderBy 100 year == 0 divisibleBy400 = remainderBy 400 year == 0 in divisibleBy4 && (not divisibleBy100 || (divisibleBy100 && divisibleBy400))
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.