|
|
|
|
|
by nostrademons
6487 days ago
|
|
That's not much harder in C or Java, given appropriate enum declarations: public int days_in_month(month) {
switch(month) {
case JAN:
case MAR:
case MAY:
case JUL:
case AUG:
case OCT:
case DEC:
return 31
case APR:
case JUN:
case SEPT:
case NOV:
return 30;
case FEB:
if(leap_year()) return 29; else return 28;
}
}
Pattern-matching really shines when you need to destructure the argument, then do something similar to the result regardless of which case resulted in the destructuring. For example, compare red-black trees written in Java:http://www.docjar.com/html/api/java/util/TreeMap.java.html vs. those written in Haskell: http://www.cse.unsw.edu.au/~dons/data/RedBlackTree.html The rebalancing code in Java is 80 lines long. The rebalancing code in Haskell is 6. |
|
Maybe something like this (in pseudo-ML):
That's a little complicated to determine with pattern matching or via a database, but doing it in a series of nested if statements would get mind boggling.