File tree Expand file tree Collapse file tree 1 file changed +45
-4
lines changed
exercises/practice/leap/.approaches Expand file tree Collapse file tree 1 file changed +45
-4
lines changed Original file line number Diff line number Diff line change 11# Introduction
22
3- There are various idiomatic approaches to solve Leap, such as
3+ There are various idiomatic approaches to solve Leap.
4+ All approaches listed below check for divisibility by 4, 100, and 400.
5+ However, they differ in the ways in which they combine these checks.
46
5- - constructing [ a logical expression] [ logical-expression ] ,
6- - using [ guards] [ guards ] , and
7- - using [ a conditional expression] [ conditional-expression ] .
7+
8+ ## Approach: a logical expression
9+
10+ ``` haskell
11+ isLeapYear :: Integer -> Bool
12+ isLeapYear year = divisibleBy 4 && (not (divisibleBy 100 ) || divisibleBy 400 )
13+ where
14+ divisibleBy d = year `mod` d == 0
15+ ```
16+
17+ [ Read more about this approach] [ logical-expression ] .
18+
19+
20+ ## Approach: use guards
21+
22+ ``` haskell
23+ isLeapYear :: Integer -> Bool
24+ isLeapYear year
25+ | indivisibleBy 4 = False
26+ | indivisibleBy 100 = True
27+ | indivisibleBy 400 = False
28+ | otherwise = True
29+ where
30+ indivisibleBy d = year `mod` d /= 0
31+ ```
32+
33+ [ Read more about this approach] [ guards ] .
34+
35+
36+ ## Approach: a conditional expression
37+
38+ ``` haskell
39+ isLeapYear :: Integer -> Bool
40+ isLeapYear year =
41+ if divisibleBy 100
42+ then divisibleBy 400
43+ else divisibleBy 4
44+ where
45+ divisibleBy d = year `mod` d == 0
46+ ```
47+
48+ [ Read more about this approach] [ conditional-expression ] .
849
950
1051## General guidance
You can’t perform that action at this time.
0 commit comments