Erlang Thursday – calendar:is_leap_year/1

Today’s Erlang Thursday is on calendar:is_leap_year/1.

calendar:is_leap_year/1 takes a non-negative integer value representing a year, and will return true if that year is a leap year, or false otherwise.

% false
% true
% false
% true
% false
% true

By having a built in function as part of the core Erlang libraries, it means you don’t have to code up the rules, or even go lookup the rules to remember how the century years are determined to be leap years or not.

And if you do pass in a negative number for the year, Erlang will raise an exception, as there are no clauses which match a negative number for the year.

% ** exception error: no function clause matching calendar:is_leap_year(-1) (calendar.erl, line 183)
% ** exception error: no function clause matching calendar:is_leap_year(-4) (calendar.erl, line 183)


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.