#lang racket (require rackunit) (provide get) ; -------------------------------------------------------------------------- ; EXERCISE 1: get (implements Racket's list-ref) ; -------------------------------------------------------------------------- (define get (lambda (los n) (if (null? los) (error 'nth "no such position") ; "nth: no such position" (if (zero? n) (first los) (get (rest los) (sub1 n)))))) (check-equal? (get '(a b c) 2) 'c) (check-equal? (get '(d c b a z) 1) 'c) ; The following test asserts that a certain call to nth should cause ; an error. Notice that we wrap the call in a lambda expression. ; (check-exn exn:fail? (lambda () (get '(d c b a z) 5))) ; ---------------------------------------------------------------------------