you are using and
properly, but your logic is wrong and should not be using and
(defn leap
[year]
(cond (zero? (mod year 400)) true
(zero? (mod year 100)) false
(zero? (mod year 4)) true
:default false))
(this is according to the rules for leap years in the Gregorian Calendar as listed on the wikipedia page for Leap Year).
I have avoided any usage of nested logic operators because the purpose of cond
is to simplify what would otherwise be a complex nested conditional into a linear sequence of choices where the first appropriate choice is selected.
Ideally one should be using a library like clj-time for any time / date logic, because these things are always much harder than anticipated to do properly and generally.
Additionally, one could use condp
, though in this case I think it obfuscates more than it clarifies:
(condp #(zero? (mod %2 %))
year
400 true
100 false
4 true
false)