
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Haskell Program to Check Leap Year
In Haskell, we can check whether a given year is a leap year is not using simple boolean expression. A leap year is a year that has an extra day (February 29th) compared to a normal year.
For example, 2004 is a leap year.
To determine if a year is a leap year or not, there are a few rules that must be followed.
Algorithm
Step 1 ? The isLeapYear function is defined
Step 2 ? Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. In the main function, a year value is defined and passed to isLeapYear function. The result of the function is then printed.
Step 3 ? The variable named, "year" is being initialized. It will hold the year which is to be checked whether it is leap year or not.
Step 4 ? Then, if else statement is used to print the result to the console using ?putStrLn' statement after the function is called.
Example 1
In this example, the isLeapYear function takes an integer (representing a year) as input and returns a Boolean indicating whether or not the year is a leap year. It uses pattern matching to determine whether the year is a leap year based on the following rules ?
If the year is evenly divisible by 400, then it is a leap year.
If the year is evenly divisible by 100 but not by 400, then it is not a leap year.
If the year is evenly divisible by 4 but not by 100, then it is a leap year.
If none of the above conditions are met, then it is not a leap year.
isLeapYear :: Int -> Bool isLeapYear year | year `mod` 400 == 0 = True | year `mod` 100 == 0 = False | year `mod` 4 == 0 = True | otherwise = False main :: IO () main = do let year = 2004 if isLeapYear year then putStrLn $ show year ++ " is a leap year." else putStrLn $ show year ++ " is not a leap year."
Output
2004 is a leap year.
Example 2
In this example, a single Boolean expression is used to determine if the year is a leap year. It checks if the year is evenly divisible by 4 and if it is not evenly divisible by 100 or if it is evenly divisible by 400. This implementation is more concise and uses fewer lines of code. The result is the same as the original implementation, but the logic is expressed more succinctly.
isLeapYear :: Int -> Bool isLeapYear year = (year `mod` 4 == 0) && (year `mod` 100 /= 0 || year `mod` 400 == 0) main :: IO () main = do let year = 2004 if isLeapYear year then putStrLn $ show year ++ " is a leap year." else putStrLn $ show year ++ " is not a leap year."
Output
2004 is a leap year.
Conclusion
In Haskell, we can define a function that determines if a year is a leap year or not, using the following rules ?
If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
The year is a leap year (it has 366 days).
The year is not a leap year (it has 365 days).