Search code examples
functional-programmingsml

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


Solution

  • 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