diff --git a/list.rkt b/list.rkt index ff23f45..cfe39c3 100644 --- a/list.rkt +++ b/list.rkt @@ -84,4 +84,12 @@ (provide values->list) (define-syntax (values->list stx) (syntax-case stx () - [(_ values-expr) #'(call-with-values (λ () values-expr) list)])) \ No newline at end of file + [(_ values-expr) #'(call-with-values (λ () values-expr) list)])) + + +(define+provide/contract (sublist xs i j) + (list? (and/c integer? (not/c negative?)) (and/c integer? (not/c negative?)) . -> . list?) + (cond + [(> j (length xs)) (error 'sublist (format "ending index ~a exceeds length of list" j))] + [(>= j i) (take (drop xs i) (- j i))] + [else (error 'sublist (format "starting index ~a is larger than ending index ~a" i j))])) diff --git a/tests.rkt b/tests.rkt index ab1602d..9e2ed8a 100644 --- a/tests.rkt +++ b/tests.rkt @@ -156,3 +156,8 @@ (check-equal? (slicef-at (range 5) odd? #t) '((1 2) (3 4))) (check-equal? (slicef-at (range 5) procedure?) '((0 1 2 3 4))) (check-exn exn:fail:contract? (λ() (slicef-at (range 5) 3))) + + +(check-equal? (sublist (range 5) 0 0) '()) +(check-equal? (sublist (range 5) 0 1) '(0)) +(check-equal? (sublist (range 5) 0 5) '(0 1 2 3 4)) \ No newline at end of file