#lang planet dyoo/whalesong/base
(require (for-syntax racket/base))
(provide first second third fourth fifth sixth seventh eighth ninth
rest)
(define (rest x)
(cond
[(pair? x)
(cdr x)]
[else
(raise-type-error 'rest
"list with at least one element"
x)]))
(define-syntax (define-list-selectors stx)
(syntax-case stx ()
[(_ [(name ordinal) ...])
(with-syntax ([(offset ...)
(build-list (length (syntax->list #'(name ...)))
(lambda (i) i))])
#'(begin
(define (name p)
(pair-ref p offset 'name 'ordinal p))
...))]))
(define (pair-ref x offset name ordinal original)
(cond
[(pair? x)
(cond
[(= offset 0)
(car x)]
[else
(pair-ref (cdr x) (sub1 offset) name ordinal original)])]
[else
(raise-type-error name
(format "list with ~a elements" ordinal)
original)]))
(define-list-selectors [[first one]
[second two]
[third three]
[fourth four]
[fifth five]
[sixth six]
[seventh seven]
[eighth eight]
[ninth nine]])