Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard ML: Ugly isLeapYear Function

I'm trying to write a function in Standard ML using case of but I end up with code that screams for a bunch of if / else's. Is this just a poor candidate for using case of or is there a better way of setting this up so I'm not constantly mapping booleans to booleans?

Here's my code:

fun isLeapYear(y) =
    case (y mod 400 = 0) of
        true => true |
        false => (case ((y mod 100 = 0)) of
            true => false |
            false => (case (y mod 4 = 0) of
                true => true |
                false => false));

Thanks for the help, bclayman

like image 315
anon_swe Avatar asked May 08 '26 09:05

anon_swe


1 Answers

If you don't care about a few extra instructions:

fun isLeapYear(y) = case (y mod 400, y mod 100, y mod 4) of
    (0, _, _) => true 
  | (_, 0, _) => false
  | (_, _, 0) => true
  | (_, _, _) => false
like image 162
seanmcl Avatar answered May 11 '26 15:05

seanmcl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!