On May 1, 2009, at 12:30 PM, Ramana Kumar wrote:

If I understand correctly, this is the 90% solution

You can start with:

(define (proc x stx)
  (if (pair? x) (car x) (error 'car "not a pair" p stx)))

(define-syntax car+
  (lambda (x)
    (with-syntax ([stx x])
      (syntax-case x ()
        [(_ e)
         #'(proc e #'stx)]
        [id (identifier? #'id)
         #'(lambda (x) (proc x #'stx))]))))

which avoids excessive code growth at each use of car+.

Of course you probably want to do cdr too, and you don't want
proc to be visible, so, you do a little abstraction there.

(define-syntax define-pair-checked ;;; can't think of good names
  (syntax-rules ()
    [(_ car+ car)
     (begin
       (define (proc x stx)
         (if (pair? x) (car x) (error 'car "not a pair" p stx)))
       (define-syntax car+
         (lambda (x)
           (with-syntax ([stx x])
             (syntax-case x ()
               [(_ e)
                #'(proc e #'stx)]
               [id (identifier? #'id)
                #'(lambda (x) (proc x #'stx))])))))]))

(define-pair-checked car+ car)
(define-pair-checked cdr+ cdr)

You can take this as far as you please.

Aziz,,,

Reply via email to