2009/6/2 lowly coder lowlycoder@huoyanjinjing.com:
Suppose I have the following:
(define-type p3 x y z) (define (with-cons c func) (func (car c) (cdr c)))) (define (with-p3 p func) (func (p3-x p) (p3-y p) (p3-z p))) (define p (cons (make-p3 1 2 3) (make-p3 4 5 6)))
Now, ideally I want to write:
(destructure-bind (cons (p3 x1 y1 z1) (p3 x2 y2 z2)) (make-point3 (+ x1 x2) (+ y1 y2) (+ z1 z2)))
How can I do this with with-cons / with-p3? (It seems I can only go done one level of nesting).
*sigh* The naming will be confusing because of your previous choices, but...
(with-cons p (lambda (p1 p2) (with-p3 p1 (lambda (x1 y1 z2) (with-p3 p2 (lambda (x2 y2 z2) (make-point3 (+ x1 x2) (+ y1 y2) (+ z1 z2)) ))))))
If you are implementing a bunch of dyadic operations you can of course encapsulate this via recursive application of the pattern. This is better than a macro because it is, in fact, more flexible; to say nothing of the phasing issues that can come into play when you start building up a large macro library.
This is an example of a general Scheme style rule which doesn't seem to get propagated so much anymore in c.l.s: when given a choice between using a function and a macro, using a function is almost always the right choice. It is certainly true until you learn just how powerful LAMBDA and full TCO really are.
david rush