From: Antony William Iorio
Subject: student new to recursion needs help!
Date: 
Message-ID: <7kpj75$m5b$1@emu.cs.rmit.edu.au>
I am trying to return the last element in a list, but my code returns nil!
(I guess this is technically correct but not what I want)
I know their is something I am missing and I can see why it doesn't work
but my little brain just can't get it!
here is my code

(defun last (m)
(if (null m)
nil
(first (last (rest m)))) 
     
Can anyone take a moment of their time to help me? I would appreciate it

From: R. Matthew Emerson
Subject: Re: student new to recursion needs help!
Date: 
Message-ID: <873dzjimqx.fsf@nightfly.apk.net>
Antony William Iorio <·······@yallara.cs.rmit.edu.au> writes:

> I am trying to return the last element in a list, but my code returns nil!
> (I guess this is technically correct but not what I want)

Assume that e is an element of a proper list.  If (cdr e) is nil, then
(car e) is the last element of the list.  Otherwise, find the last
element of (cdr e).

[don't you have a TA or something?]

> I know their is something I am missing and I can see why it doesn't work
> but my little brain just can't get it!
> here is my code
> 
> (defun last (m)
> (if (null m)
> nil
> (first (last (rest m)))) 
From: Steve Long
Subject: Re: student new to recursion needs help!
Date: 
Message-ID: <37740323.30F214AD@boeing.com>
R. Matthew Emerson wrote:
> 
> Antony William Iorio <·······@yallara.cs.rmit.edu.au> writes:
> 
> > I am trying to return the last element in a list, but my code returns nil!
> > (I guess this is technically correct but not what I want)
> 
> Assume that e is an element of a proper list.  If (cdr e) is nil, then
> (car e) is the last element of the list.  Otherwise, find the last
> element of (cdr e).
> 
> [don't you have a TA or something?]
> 
> > I know their is something I am missing and I can see why it doesn't work
> > but my little brain just can't get it!
> > here is my code
> >
> > (defun last (m)
> > (if (null m)
> > nil
> > (first (last (rest m))))

Why not just (lastcar theList)?
From: Lars Marius Garshol
Subject: Re: student new to recursion needs help!
Date: 
Message-ID: <wkaetrqgwo.fsf@ifi.uio.no>
* Antony William Iorio
|
| I know their is something I am missing and I can see why it doesn't
| work but my little brain just can't get it!

I guess this is homework (if it is, please be honest enough to say so!),
but a little hint can't hurt. 

| (defun last (m)
| (if (null m)
| nil
| (first (last (rest m)))) 

First, you need to indent this properly:

(defun last (m)
  (if (null m)
      nil
    (first (last (rest m)))) 

If you analyze this function, you see that it will either return nil,
or the first of the last of the rest of m. In the latter case it will
need to call itself and then return either nil or call itself again.

If you think about this it means that the only thing last ever _can_
return is nil, no matter which way it goes.

Then you can look at Matthew Emerson's hint and you should have some
idea of how to do this.

--Lars M.
From: Johan Kullstam
Subject: Re: student new to recursion needs help!
Date: 
Message-ID: <uwvwvm1oa.fsf@res.raytheon.com>
Antony William Iorio <·······@yallara.cs.rmit.edu.au> writes:

> I am trying to return the last element in a list, but my code returns nil!
> (I guess this is technically correct but not what I want)
> I know their is something I am missing and I can see why it doesn't work
> but my little brain just can't get it!
> here is my code
> 
> (defun last (m)
> (if (null m)
> nil
> (first (last (rest m)))) 
>      
> Can anyone take a moment of their time to help me? I would
> appreciate it

how about this?

(defun last (lyst)
   (when lyst (elt lyst (- (length lyst) 1))))

it ain't pretty but it does work.
        
-- 
johan kullstam
From: Adan Josuaius
Subject: Re: student new to recursion needs help!
Date: 
Message-ID: <7ksdv1$9k0$1@utl2.reitoria.utl.pt>
>(defun last (m)
>(if (null m)
>nil
>(first (last (rest m))))

(last '(1 2))  --> (first (last '(1))) --> (first (last nil)) --> (first
nil) --> nil

    Your recursion is not correct, when you reach the end of list, you have
not the last element, but only nil.
    Just do this little modification:

(defun last (m)
  (cond ((null m) nil)
            ((null (rest m)) (first m)) ;this way, you can see when a
                                                  ;list is empty before it
really became empty
           (t (last (rest m)))))

    Sorry for my english, but is not my natural language

--

            Joao Salvado
          (Adan  Josuaius)

      --------------------------
       INTELIGENCIA ARTIFICIAL
      Instituto Superior Tecnico
      --------------------------
       From IMAGINARIUM PALACE
      --------------------------

 "Minds are like parachutes, They only function when they
  are open"

--> http://alfa.ist.utl.pt/~l42633 <--
----> ············@rnl.ist.utl.pt <---
----------> ICQ: 16865680 <-----------