Dan's Thoughts Thinking somewhat carefully

2Aug/090

Exercise 2.17 of 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 ls)
  (let ((last-pair-index (- (length ls) 1)))
    (list-ref ls last-pair-index)))

> (last-pair (list 1 2 3 4 5))
5

Filed under: lisp, SICP Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

Spam protection by WP Captcha-Free

No trackbacks yet.