Skip to content

Commit 2596909

Browse files
authored
leap approaches: Present the approaches in the overview document (#1147)
1 parent b2548e1 commit 2596909

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

exercises/practice/leap/.approaches/introduction.md

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
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

0 commit comments

Comments
 (0)