Re:Erlang will replace Java in the next 2 years as the stand...
by anamax on Wednesday September 10, 2008 @08:27PM
(#300911)
I wasn't thinking Java. I was thinking lisp or python.
[ Parent
| on HN ]
Re:Erlang will replace Java in the next 2 years as the stand...
by silentbicycle on Thursday September 11, 2008 @03:09PM
(#301655)
For one thing, the pattern matching used in Erlang, ML, and Haskell can be quite easy to read.
For example, something like
let days_in_month m = match m with
| Jan | Mar | May | Jul | Aug | Oct | Dec -> 31
| Apr | Jun | Sept | Nov -> 30
| Feb -> if leap_year () then 29 else 28
It takes a small amount of context (all "| Val" sections without a -> ... side match together), but it's a pretty direct correspondence.
Re:Erlang will replace Java in the next 2 years as the stand...
by nostrademons on Thursday September 11, 2008 @04:49PM
(#301750)
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.
Re:Erlang will replace Java in the next 2 years as the stand...
by silentbicycle on Thursday September 11, 2008 @06:55PM
(#301852)
I couldn't think of a better example at the time. Something that simultaneously matched several fields (whether destructured from one variable or not) and/or had guards would be a better example.
Maybe something like this (in pseudo-ML):
(* Look up order code for a Sci-Fi GadgetTM, units are in cm *)
let gadget_order_code width depth height color has_laser =
match (width depth height color has_laser) =
(* The original, simple code *)
| 5, 2, 1, Red, true -> "ZAP-X"
(* Most common width, color *)
| 4, d, h, Blue, true -> sprintf "ZAP-4-%d%d+" (str d) (str h) "+"
(* C for Custom Color *)
| 4, d, h, c, false -> "ZAP-4C-%d%d-%c" (str d) (str h) (color_code c)
(* Orange lasers are discontinued *)
| w, d, h, c, true when (w + d + h) < 15 and c != Orange ->
"ZAP-S-%d%d%d-%c" (str w) (str d) (str h) (color_code c)
| _, _, _, _, _ -> failwith "Unavailable"
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.
Re:Erlang will replace Java in the next 2 years as the stand...
by silentbicycle on Thursday September 11, 2008 @09:43PM
(#301997)
(oops. lost the sprintfs...)