A maybe-interesting paper by Aaron Mark Ucko, “Predicate Dispatching in the Common Lisp Object System, argues that predicate dispatching gives the best of the usual CLOS-style type dispatching and the ML/Haskell style pattern matching worlds.
(defpdmethod hailstone (n) ((evenp n)) (/ n 2)) (defpdmethod hailstone (n) ((oddp n)) (+ (* n 3) 1)) (defun hsseq (n) (if (= n 1) ’(1) (cons n (hsseq (hailstone n)))))Posted by jjwiseman at October 29, 2004 01:18 PM
Predicate dispatching is the best thing since sliced bread. Check out JPred: http://www.cs.ucla.edu/~todd/jpred/.
Actually Haskell has this. They're called "pattern guards":
hailstone x | even x = x / 2
hailstone x = (3 * x) + 1
Ashley, that's not entirely true. If you change the order of those declarations in Haskell it won't work anymore. With predicate dispatching it would still work, because the clause with the 'even' is more specific (it implies the other, but the inverse isn't true).
Posted by: Me on November 18, 2004 06:55 PM