; ====================================================================== ; ; Structure and Interpretation of Computer Programs ; (trial answer to excercises) ; ; 计算机程序的构造和解释(习题试解) ; ; created: code17 07/14/05 ; modified: ; (保持内容完整不变前提下,可以任意转载) ; ======================================================================
;; SICP No.2.17
(define (last-pair items) (let ((len (length items))) (cond ((= len 0) (error "empty list")) ((= len 1) items) (else (last-pair (cdr items))))))
;; Test-it: ;; Welcome to MzScheme version 209, Copyright (c) 2004 PLT Scheme, Inc. ;; > (last-pair (list 23 72 149 34)) ;; (34) ;; > (last-pair ()) ;; empty list ;; > (last-pair (list 31)) ;; (31)
|