Rebol3 Code Examplex


Leap year

Determine whether a year is a leap year.

Rebol [
    title: "Rosetta code: Leap year"
    file:  %Leap_year.r3
    url:   https://rosettacode.org/wiki/Leap_year
]
leap-year?: func [
    {Returns true if the specified year is a leap year; false otherwise.}
    year [date! integer!] 
][
    ;; If the input is a date!, extract just the year component
    if date? year [year: year/year]
    ;; Validate that the year is not negative
    assert [not negative? year]
    ;; If divisible by 100...
    if zero? remainder year 100 [
        ;; ... it's a leap year only if divisible by 400
        return zero? remainder year 400
    ]
    ;; Otherwise, it's a leap year if divisible by 4
    zero? remainder year 4
]
print "Leap year examples:"
foreach year [
    1600 ;(divisible by 400, so leap year)
    1704
    1808
    1904
    2000 ;(divisible by 400, leap year even though it’s a century)
    2004
    2008
    2012
    2016
    2020
    2024
    2028
    2400 ;(way in the future, still divisible by 400)
][
    print [tab year leap-year? year]
]
print "Not leap year examples:"
foreach year [1700 1800 1900 2025][
    print [tab year leap-year? year]
]

Output:

Leap year examples:
     1600 true
     1704 true
     1808 true
     1904 true
     2000 true
     2004 true
     2008 true
     2012 true
     2016 true
     2020 true
     2024 true
     2028 true
     2400 true
Not leap year examples:
     1700 false
     1800 false
     1900 false
     2025 false