;; ex-2.17 (define (last-pair items) (let ((rest (cdr items))) (if (null? rest) items (last-pair rest)))) ;; Testing (last-pair (list 23 72 149 34)) ; (34)
<< Previous exercises (2.14-2.15-2.16) | Index | Next exercise (2.18) >>
solution which doesn't fail for '() (define (last-pair items) (define (iter items result) (if (null? items) result (iter (cdr items) items))) (iter items items))
A simple but ingenious code
(define(lastPair L)
(if(=(length L)1) (car L)
(lastPair (cdr L))))
The answer was meant to return the last item of a list as a list. So you must modify the return value to return a list.
(define (last-pair l) (cond ((null? l) l) ((null? (cdr l)) (list (car l))) (else (last-pair (cdr l))))) ;; test (last-pair (list 23 72 149 34)) ;;= (34) (last-pair '()) ;;= ()
Regarding the first solution. We need to return a list, not a particular element. 1 != '(1)
;; returns the list that contains the last element ;; of a given non-empty list (define (last-pair L) (if (null? (cdr L)) L (last-pair (cdr L)))) (last-pair (list 1)) ;; '(1) (last-pair (list 1 2)) ;; '(2) (last-pair (list 1 2 3)) ;; '(3) (last-pair (list 1 2 3 4)) ;; '(4)
jz