A lightweight `bind' macro

In my recent work I found myself writing a lot of code that looked like the following:

(destructuring-bind (x y)
  (destructuring-bind (x1 y1 w h)
    (let ((x2 (+ x1 w))
          (y2 (+ y1 h)))
      (and (>= x x1) (<= x x2) (>= y y1) (<= y y2)))))

The above example is a bit contrived, for effect, but hopefully conveys the general idea.

What I wanted was a Clojure style `let' which could destructure inline. There was one Common Lisp implementation (http://common-lisp.net/project/metabang-bind/), which had some nice features, but it was bit much for my needs. So I came up with my own lightweight bind macro (see below), which lets me rewrite the above example as:

(bind (((x y) point)
       ((x1 y1 w h) frame)
       (x2 (+ x1 w))
       (y2 (+ y1 h)))
  (and (>= x x1) (<= x x2) (>= y y1) (<= y y2)))

Way better, IMHO.

Oh, here's the `bind' macro in its entirety:

(defmacro bind (clauses &body body)
  "This macro combines the behaviour of the forms `let*', `destructuring-bind',
and `multiple-value-bind', permitting the following style of binding form:
  (bind (((:values m n) (values 10 20))
         ((a b &key (c 10)) '(1 2))
         (x 5))
    (+ x a b c m n))
  => 48
This is a more limited and lightweight implementation of some ideas from
metabang-bind (http://common-lisp.net/project/metabang-bind/)."
    ((null clauses) `(progn ,@body))
    ((and (listp (caar clauses)) (eq (caaar clauses) :values))
     `(multiple-value-bind ,(cdaar clauses)
          ,@(cdar clauses)
        (bind ,(cdr clauses) ,@body)))
    ((listp (caar clauses))
     `(destructuring-bind ,(caar clauses)
          ,@(cdar clauses)
        (bind ,(cdr clauses) ,@body)))
     `(let (,(car clauses))
        (bind ,(cdr clauses) ,@body)))))

community funded clojure

Rich Hickey posted a compelling message about funding the future development of Clojure.

I donated, and urge all Lispers to consider the same. Clojure maybe one of the best things to happen to Lisp in a long while, and it and Rich are most deserving of our continued support.

Donate at clojure.org.

ECL on iPhone (redux)

A while ago Red Daly posted about some great initial work he had done in porting ECL to the iPhone. I contacted him, and worked to clean up some of the external dependencies, and got ECL to run on the simulator and device (with SLIME). I put together an updated Git repo with all my changes, and finally got around to posting about it.


Hopefully, this will be of use to others.