previous | index | next

Getting the First n Elements of a List

Q: The first zero elements of a list comprise what list?

A: The empty list.

Q: The first n elements of a list is what list? (Hint: Answer in terms of the head and the tail.)

A: The list created by consing the head onto the first n-1 elements of the tail.

     (define first-elements-of
       (lambda (n list)
         (if (= n 0)
             '()
             (cons (car list) 
                   (first-elements-of (- n 1) 
                                      (cdr list))))))

previous | index | next