SICP 全笔记

Exercise 2.17. Define a procedure last-pair that returns the list that contains only the last element of a given (nonempty) list:

(last-pair (list 23 72 149 34))
(34)

(define (last-pair lst)
  (cond ((null? (cdr lst)) lst)
        (else (last-pair (cdr lst)))))


;;; tests begin
(load "../testframe.scm")

(assertequal? (last-pair '(1)) '(1))
(assertequal? (last-pair '(a b c d)) '(d))