Exercises in this lecture   Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home   

Exercise solution:
A counterpart to list-tail


Here are my functions:

(define (my-list-tail lst n)
  (if (= n 0)
      lst
      (my-list-tail (cdr lst) (- n 1))))

(define (list-prefix lst n)
  (if (= n 0)
      '()
      (cons (car lst) (list-prefix (cdr lst) (- n 1)))))

Notice that we must allocate memory to the result in list-prefix, whereas my-list-tail returns part of the original list (without allocating additional space).