minor code improvements, added ninth and tenth

svn: r8364
This commit is contained in:
Eli Barzilay 2008-01-18 16:04:43 +00:00
parent a9f76e95d6
commit cd239fc23c
2 changed files with 49 additions and 53 deletions

View File

@ -1,14 +1,6 @@
#lang scheme/base
(module list scheme/base
(provide first
second
third
fourth
fifth
sixth
seventh
eighth
(provide first second third fourth fifth sixth seventh eighth ninth tenth
last
rest
@ -18,10 +10,9 @@
empty?)
(define (first x)
(unless (and (pair? x)
(list? x))
(raise-type-error 'first "non-empty list" x))
(car x))
(if (and (pair? x) (list? x))
(car x)
(raise-type-error 'first "non-empty list" x)))
(define-syntax define-lgetter
(syntax-rules ()
@ -31,7 +22,8 @@
(let loop ([l l0] [pos npos])
(if (pair? l)
(if (eq? pos 1) (car l) (loop (cdr l) (sub1 pos)))
(raise-type-error 'name (format "list with ~a or more items" npos) l0)))
(raise-type-error
'name (format "list with ~a or more items" npos) l0)))
(raise-type-error 'name "list" l0)))]))
(define-lgetter second 2)
(define-lgetter third 3)
@ -40,22 +32,22 @@
(define-lgetter sixth 6)
(define-lgetter seventh 7)
(define-lgetter eighth 8)
(define-lgetter ninth 9)
(define-lgetter tenth 10)
(define (last x)
(unless (and (pair? x)
(list? x))
(raise-type-error 'last "non-empty list" x))
(let loop ([x x])
(if (pair? (cdr x))
(loop (cdr x))
(car x))))
(define (last l)
(if (and (pair? l) (list? l))
(let loop ([l l])
(if (pair? (cdr l))
(loop (cdr l))
(car l)))
(raise-type-error 'last "non-empty list" l)))
(define (rest x)
(unless (and (pair? x)
(list? x))
(raise-type-error 'rest "non-empty list" x))
(cdr x))
(define (rest l)
(if (and (pair? l) (list? l))
(cdr l)
(raise-type-error 'rest "non-empty list" l)))
(define cons? (lambda (x) (pair? x)))
(define empty? (lambda (x) (null? x)))
(define empty '()))
(define cons? (lambda (l) (pair? l)))
(define empty? (lambda (l) (null? l)))
(define empty '())

View File

@ -461,6 +461,10 @@ Like @scheme[assoc], but finds an element using the predicate
@defproc[(eighth [lst list?]) any]{Returns the eighth element of the list.}
@defproc[(ninth [lst list?]) any]{Returns the ninth element of the list.}
@defproc[(tenth [lst list?]) any]{Returns the tenth element of the list.}
@defproc[(last [lst list?]) any]{Returns the last element of the list.}
@; ----------------------------------------