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 list1)
(if
(null? list1)
'()
(if
(null? (cdr list1))
(car list1)
(last-pair (cdr list1)))))
(last-pair (list 1 2 3 4 5))
(last-pair (list 1))
(last-pair ())
(last-pair '())

view raw

s217.scm

hosted with ❤ by GitHub

 

Discover more from Gaurav Sharma's Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading