tags:

views:

132

answers:

7

how can I write a function to take the last element of the list?

+3  A: 

find the last of a list:

(define (last l)
    (cond ((null? (cdr l)) (car l))
          (else (last (cdr l)))))

use map to map last to a list:

(map last '((a b) (c d) (e f)))
==> (b d f)

so a new function:

(define (last-list l)
  (map last l)
  )

(last-list '((a b) (c d) (e f)))
==> (b d f)
linjunhalida
This will return the last element of every sublist... not what he's asking, as best as I can tell. Yes?
Steven Schlansker
It is what he's asking Steven, look carefully at the original question (not the code.)
McPherrinM
+2  A: 

The code you've written - to take the last element of a list - is correctly returning the last element of the list. You have a list of lists. There is an outer list

(x y z)

where

x = (a b)
y = (c d)
z = (e f)

So you're getting the last element of the list, z, which is (e f)

Did you want your last function to do something different? If you want it to return the last element of the last nested list, you need to change your base case. Right now you return the car. Instead, you want to check if the car is a list and then call your nested-last function on that.

Make sense?

Steven Schlansker
+1  A: 

Your last function is good, but you have to think about what you want to do with it now.

You have a list of lists, and you want to take the last of all those.

So recurse down your list applying it each time:

(define (answer lst)
   (cond ((null? (cdr l)) null)
         (else (cons (last (car l)) (answer (cdr lst))))
McPherrinM
A: 

Yet another possibility:

(define (last thelist) 
    (if 
        (null? (cdr thelist)) (car thelist)
        (last (cdr thelist))))

(define (all-last lists) (map last lists))

Edit: just saw that you don't know map, and want a solution without it:

(define (all-last lists) 
    (if
        (null? lists) `()
        (cons (last (car lists)) (all-last (cdr lists)))))

As far as getting an empty list goes, I'd guess you're trying to use this map-like front-end with your original definition of last, whereas it's intended to work with the definition of last I gave above. Try the following definitions:

(define (last thelist) (if 
                 (null? (cdr thelist)) (car thelist)
                 (last (cdr thelist))))

(define (all-last lists) (if
                      (null? lists) `()
                      (cons (last (car lists)) (all-last (cdr lists)))))

and running a quick test:

(all-last `((a b) (c d) (e f)))

The result should be:

(b d f)

Jerry Coffin
+3  A: 

May not be the most efficient, but certainly one of the simplest:

(define (last lst)
  (car (reverse lst)))

Examples:

(last '(1 2 3 4)) => 4    
(last '((a b) (b c) (d e))) => (d e)
Vijay Mathew
+2  A: 

Read HtDP and follow the recipe. Your function would write itself man.

grettke
A: 
(define last
  (lambda (ls)
    (list-ref ls (- (length ls) 1))))
ecounysis