s-99-01


 (define (my-last l) 
   (if (null? (cdr l)) 
     (car l) 
     (my-last (cdr l)))) 

Alternatively, a quasi-stream-like delay-by-one solution that arbitrarily assumes the last element of an empty list is an empty list (on which, I believe, the previous solution explodes, although the problem may allow assuming that the list is never empty).

 (define (get-last lst) 
  
   (define (get-last' fst rst) 
     (if (null? rst) 
       fst 
       (get-last' (car rst) (cdr rst)))) 
  
   (get-last' '() lst)) 
  
 guile> (get-last '()) 
 () 
 guile> (get-last '(1)) 
 1 
 guile> (get-last '(1 2)) 
 2 
 guile> 

rmn

This solution can also be written in terms of a fold.

  
 (define (my-last xs) 
   (fold-left (lambda (a x) x) '() xs)) 
  
 (my-last '(a b c d)) ; => d 
 (my-last '())        ; => ()