Hacker News new | ask | show | jobs
by silentbicycle 6483 days ago
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.
1 comments

(oops. lost the sprintfs...)