Quantcast
Channel: User Bartek Banachewicz - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 14

Answer by Bartek Banachewicz for Idiomatic Leap years in haskell

$
0
0

One thing that always comes to my mind when seeing code like that is one of the first principles in programming that everyone learns.

Don't repeat yourself.

In your code, the part that stands out is mod year n == 0; let's get away with it first:

isLeapYear :: Int -> BoolisLeapYear year = divisibleBy 400 || (divisibleBy 4 && not $ divisibleBy 100)    where        divisibleBy x = mod year x

Using such short helpers is very convenient in Haskell. In my opinion it's one of the most important reasons for the code staying readable (contrary to some newcomers' opinions who just pile up code in one giant expression).

Now, what I don't like here is the full name for the year; I know, I know, it's descriptive, but bear with me:

type Year = IntisLeapYear :: Year -> BoolisLeapYear y = divisibleBy 400 || (divisibleBy 4 && not (divisibleBy 100))    where        divisibleBy x = mod y x == 0

Now, this is the point where I'd really, really leave it be.


Viewing all articles
Browse latest Browse all 14

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>