my attempt to do the exercises in sicp.

Wednesday, July 16, 2008

sicp exercise 2.17



; 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 (lambda (items)
  (if (null? (cdr items)) (car items)
      (last-pair (cdr items)))))

(display (last-pair (list 1 2 3 4)))(newline)
(display (last-pair (list 1 3 2 1 3 4 5 6)))(newline)
(display (last-pair (cons (list 1 2 3 4) (list 1 2 20 ))))(newline)
(display (last-pair (list 1 2 3 4 (list 1 2 20 ))))(newline)

No comments: