October 29, 2004
Predicate Dispatch

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
Comments

Predicate dispatching is the best thing since sliced bread. Check out JPred: http://www.cs.ucla.edu/~todd/jpred/.

Posted by: surana on October 29, 2004 02:17 PM

Actually Haskell has this. They're called "pattern guards":

hailstone x | even x = x / 2
hailstone x = (3 * x) + 1

Posted by: Ashley Yakeley on November 4, 2004 03:49 PM

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
Post a comment
Name:


Email Address:


URL:




Unless you answer this question, your comment will be classified as spam and will not be posted.
(I'll give you a hint: the answer is “lisp”.)

Comments:


Remember info?