; nth from the end

(define (nth-last n xs)
  (list-ref (reverse xs) (- n 1)))

(display (nth-last 3 '(1 2 3 4 5 6 7 8 9))) (newline)

(define (nth-last n xs)
  (list-ref xs (- (length xs) n)))

(display (nth-last 3 '(1 2 3 4 5 6 7 8 9))) (newline)

(define (nth-last n xs)
  (let loop ((n n) (rabbit xs) (greyhound xs))
    (if (positive? n) (loop (- n 1) (cdr rabbit) greyhound)
      (if (pair? rabbit) (loop n (cdr rabbit) (cdr greyhound))
        (car greyhound)))))

(display (nth-last 3 '(1 2 3 4 5 6 7 8 9))) (newline)