views:

977

answers:

4

Hi guys! help please..say i have a list ((3 4 5) (d e f) (h i j) (5 5 5 5)) ) how can i get the last element of each list in such a way that the output would look like this (5 f j 5). Thanks in advance!

A: 

Write a procedure that returns the last element of a list, then learn a little about the built-in MAP (a.k.a. MAPCAR) procedure and see if any lightbulbs go off.

Cirno de Bergerac
In common-lisp, the built-in functions LAST and MAPCAN may also be rather useful.
Pete Kirkham
+5  A: 

Assuming this is about Common Lisp, there is a function last which returns a list containing the last item of a list. If you use this function with mapcan, which applies a given function to each element of a list and returns the concatenated results, you'll get what you want.

Note though that accessing the last element of a list is an O(N) operation, so if this isn't just homework after all, you might want to consider if you can't solve the real problem more efficiently than taking the last item of each list (maybe use another datastructure instead).

sepp2k
Thanks for that answer but can you give me more information about that. like how MAPCAR is use. Sorry to ask but this just my firt attemp in developing in lisp hope you guys can help me more on this..Thanks in advance..btw, im using common lisp and sorry that i post my reply as answer.
I've added links to the documentation for the mentioned functions. Those contain examples.
sepp2k
Hi! i was able to make one the work some how the way i want.this is my code(defun lastelements(L) (if L (append (last (first L)) (lastelements(rest L))) ))But i have small problem..When i input '((4) (hello (world))) the ouput is something like this ((4) ((world))) but from example output give in our exercise it should look like this (4 (world))..Thanks in advance!
Don't append, concatenate (`last` returns a list).
sepp2k
(mapcan 'last myList)
Mike Dunlavey
Using `mapcan` is an **extremely bad idea**, especially for newbies.
Eli Barzilay
A: 

This, like most early LISPy homework problems is an exercise in thinking recursively and/or thinking in terms of induction. The way to start is to ask yourself simple questions that you can answer easily.

For example, if you had been asked to write something that gave you the first element in each list, I would thing about it this way:

Given a list of lists:

  1. What is first-element of every list in the list '() (easy - null)
  2. What is first-element of every list in the list '(a)? (easy - (a), or maybe an error)
  3. What is first-element of every list in the list '((a)) (easy - (a))
  4. What is first-element of any list in the form '(anything), where anything is a list? (easy - (first anything))
  5. What is the first element of every list in the form '(anything morestuff)? (easy - (cons (first anything) (first-element morestuff) )
  6. What is first of an atom? either the atom or an error (depends on your point of view)
  7. What is first of null? nil.
  8. What is first of a list? (car list)

From here we can start writing code:

;; here's first, meeting questions 6-8
(define first (lambda (l)
  (cond 
    ((null? l) nil) ; Q7
    ((atom? l) l)   ; Q6
    (t (car l)))))  ; Q8

;; with first we can write first-element, meeting questions 1-5
(define first-element (lambda (l)
  (cond
    ((null? l) nil) ; Q1
    ((atom? l) (first l))   ; Q2
    (t (cons (first (car l) (first-element (cdr l)))))))) ; Q4-5

Now this isn't your homework (intentionally). You should play with this and understand how it works. Your next goal should be to find out how this differs from your assignment and how to get there.

With respect to MAPCAR? Don't worry about it. You need to learn how to solve recursive problems first. Then you can worry about MAPCAR. What is the point of this assignment? To help you learn to think in this mode. Dang near everything in LISP/Scheme is solved by thinking this way.

The reason I went with all the questions to break it down into the parts that I'm worried about. If I'm given the task "how do I do foo on every item in a list?" I should answer the questions: How do I do handle null? How do handle an atom? How do I do handle on the first element on the list? How do I handle everything else? Once I've answered that, then I figure out how to actually do foo. How do I do foo on null? How do I do foo on an atom? How do I do foo on a list?

plinth
A: 

Hi, probably it is already solved, but I figured this out

; SELECT-FROM-INNER-LIST :: [list] -> [list]
(DEFUN SFIL (lst)
  (COND ((NULL lst) NIL)
        ((LISTP (FIRST lst)) (APPEND (LAST (FIRST lst)) (SFIL (REST lst))))
))

Now, this works for legit list...so if you call function SFIL with correct list.... if not, it will return NIL

hopefully this will be helpful, for anyone who finds it

Myho