Skip to content

Instantly share code, notes, and snippets.

@heathermiller
Last active June 3, 2020 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heathermiller/eb8be53bbe122ed6e9933accae97b655 to your computer and use it in GitHub Desktop.
Save heathermiller/eb8be53bbe122ed6e9933accae97b655 to your computer and use it in GitHub Desktop.
LeapYear in Unison

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.

use base.Int.mod
isLeapYear : Int -> Boolean
isLeapYear year =
if (mod year +4 == +0) && (mod year +100 .base.Universal.!= +0) || (mod year +400 == +0)
then true else false
test> isLeapYear.tests.ex1 =
check ( isLeapYear +2000 == true )
test> isLeapYear.tests.ex2 =
check ( isLeapYear +2015 == false )
test> isLeapYear.tests.ex3 =
check ( isLeapYear +1970 == false )
test> isLeapYear.tests.ex4 =
check ( isLeapYear +1996 == true )
test> isLeapYear.tests.ex5 =
check ( isLeapYear +1960 == true )
test> isLeapYear.tests.ex6 =
check ( isLeapYear +2100 == false )
test> isLeapYear.tests.ex7 =
check ( isLeapYear +1900 == false )
test> isLeapYear.tests.ex8 =
check ( isLeapYear +2400 == true )
test> isLeapYear.tests.ex9 =
check ( isLeapYear +1800 == false )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment