Re: SBCL is now faster than Java, as fast as Ocaml, and getting better



John Thingstad wrote:
I think my point was that there are many types of pattern matching.
Why should one be a part of the language and the others not?

Indeed. Looking at functional code it is clear that users benefit from
pattern matching (e.g. in SML, OCaml, Haskell, Scala, F#, Mathematica and
even Scheme) only if it is standardized and well implemented.

Seems to me to be pretty few places in the code where algebraic matching
helps the code.

Try any of the above languages and the enormous advancement made by built-in
pattern matching will be blindingly obvious.

Compare this OCaml code:

let rec intersect orig dir (l, _ as hit) (center, radius, scene) =
match ray_sphere orig dir center radius, scene with
| lam', _ when lam' >= lam -> hit
| lam', [] -> lam', unitise (orig +| lam' *| dir -| center)
| _, scenes -> List.fold_left (intersect orig dir) hit scenes

With the equivalent Common Lisp:

(defun intersect (orig dir scene)
(labels ((aux (lam normal scene)
(let* ((center (sphere-center scene))
(lamt (ray-sphere orig
dir
center
(sphere-radius scene))))
(if (>= lamt lam)
(values lam normal)
(etypecase scene
(group
(dolist (kid (group-children scene))
(setf (values lam normal)
(aux lam normal kid)))
(values lam normal))
(sphere
(values lamt (unitise
(-v (+v orig (*v lamt dir)) center)))))))))
(aux infinity zero scene)))

The considerable difference in verbosity is primarily due to pattern
matching, which is used both for destructuring (replacing
MULTIPLE-VALUE-BIND, DESTRUCTURING-BIND, CAR, CDR etc.) and for dispatch
(replacing COND etc.).

With pattern matching, the syntactic overhead is as low as possible, e.g.
destructuring a pair return value:

# let polar x y = sqrt(x *. x +. y *. y), atan2 y x;;
val polar : float -> float -> float * float = <fun>
# let r, theta = polar 3. 4.;;
val r : float = 5.
val theta : float = 0.927295218001612187

Compared to:

* (defun polar (x y)
(values (sqrt (+ (* x x) (* y y))) (atan y x)))

POLAR
* (multiple-value-bind (r theta) (polar 3.0 4.0)
(vector r theta))

#(5.0 0.9272952)

Pattern matching improves most function definitions.

--
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com/products/?u
.