Example: Last Pair in a List

The function last (a standard function in Common Lisp) returns the last pair in a linked list.

   (last '(a b c))  =  (c)

last can easily be implemented as a tail-recursive function:

(define (last lst)
  (if (pair? lst)
      (if (pair? (cdr lst))
          (last (cdr lst))
      lst) )

The last element of a list can be gotten by using car on the result of last:

   (car (last '(a b c)))  =  c

Contents    Page-10    Prev    Next    Page+10    Index