From: kazzarazza003
Subject: reverse function!!!
Date: 
Message-ID: <753f4227ff990e2232091ad3f9355c69@localhost.talkaboutprogramming.com>
Hi, I don't know how to reverse a list within a list properly (e.g (a (b c
d)) = ((d c b) a). Instead it comes out as ((b c d) a)

My code is: 

(defun reverse1 (list)
(cond ((null (car list)) nil)
(t (append (reverse1 (cdr list))
(list (car list))))))

I am guessing that there needs to be another condition but I don't know
how to say if the list is within a list. I am new to lisp. Please help! 

From: Jon Boone
Subject: Re: reverse function!!!
Date: 
Message-ID: <BD87B80F.38609%ipmonger@comcast.net>
On 2004-10-05 02:10, in article
································@localhost.talkaboutprogramming.com,
"kazzarazza003" <········@students.unisa.edu.au> wrote:

> Hi, I don't know how to reverse a list within a list properly (e.g (a (b c
> d)) = ((d c b) a). Instead it comes out as ((b c d) a)
> 
> My code is: 
> 
> (defun reverse1 (list)
> (cond ((null (car list)) nil)
> (t (append (reverse1 (cdr list))
> (list (car list))))))
> 
> I am guessing that there needs to be another condition but I don't know
> how to say if the list is within a list. I am new to lisp. Please help!

Think recursively.

--jon
From: ·······@yahoo.com
Subject: Re: reverse function!!!
Date: 
Message-ID: <1096972513.726863.71200@k17g2000odb.googlegroups.com>
Is that your homework? :0
From: kazzarazza003
Subject: Re: reverse function!!!
Date: 
Message-ID: <c5f7d38eec53fccec51aefd452b87d11@localhost.talkaboutprogramming.com>
no this is for a uni assignment that I can't get any help for from my
tutor.
From: Svein Ove Aas
Subject: Re: reverse function!!!
Date: 
Message-ID: <ck444f$fu1$3@services.kq.no>
kazzarazza003 wrote:

> no this is for a uni assignment that I can't get any help for from my
> tutor.

Homework, in other words.

Depending on your Lisp implementation, reverse may be a Lisp function with
source. Depending on your editor, you may be able to get at it by using M-.
From: Kenny Tilton
Subject: Re: reverse function!!!
Date: 
Message-ID: <jCr8d.154836$4h7.27382147@twister.nyc.rr.com>
kazzarazza003 wrote:

> Hi, I don't know how to reverse a list within a list properly (e.g (a (b c
> d)) = ((d c b) a). Instead it comes out as ((b c d) a)
> 
> My code is: 
> 
> (defun reverse1 (list)
> (cond ((null (car list)) nil)
> (t (append (reverse1 (cdr list))
> (list (car list))))))
> 
> I am guessing that there needs to be another condition but I don't know
> how to say if the list is within a list. 

Why does that matter? You want to reverse a list whether it is the 
outermost list or any nested list however deep, if I understand you.

Anyway, you can test if something is a list with listp or consp. The 
difference is: (listp nil) => T  vs. (consp nil) => nil

(defun reverse1 (list)
   (cond
    ((null (car list)) nil) ;; first problem
    (t (append (reverse1 (cdr list))
         (list (car list)))))) ;; second problem

btw, first problem you have is in the first cond clause, witness:

    (reverse1 '(nil (a b) 1 2)) => nil

The goof is testing (car list). The idea of your recursion is
to call reverse1 at each level with all but the first thing
in the list, so that eventually you get to the end of the list
and can stop recursing. But you know you have gotten to the end of
the list when the input list itself is nil, not its car. [Aside: The 
code seems to work (unless you put nil in the list) because (car nil) => 
nil.]

(defun reverse1 (input)
   (print `(r1 ,input))
   (cond
    ((null input) nil)
    ((consp input) (append (reverse1 (cdr input))
                    (list (reverse1 (car input)))))
    (t input)))

(reverse1 '(nil (a b) 1 2)) => (2 1 (A B) NIL)

Now for the core problem. You are very close. You seem to grok 
recursion, and you see that the problem is with nested lists. Take the 
minimal example:

    (reverse1 '((a b)))

...and hand-execute your algorithm. You just need to stare at a little 
and find a simple mistake. Fixing that will force a second simple 
adjustment when you get back to something mixed such as '(a b (c d) e f).

kenny




-- 
Cells? Cello? Celtik?: http://www.common-lisp.net/project/cells/
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
From: bozo
Subject: Re: reverse function!!!
Date: 
Message-ID: <J1N8d.6098$Vm1.1836@newsread3.news.atl.earthlink.net>
If this is homework, the following is probably unacceptable because it uses
reverse and mapcar:

(defun deep-reverse (x)
  (if (atom x)
      x
      (mapcar #'deep-reverse (reverse x))))

CL-USER> (deep-reverse '(1 2 (3 4) 5 (6 7 (8 9)) 10))
(10 ((9 8) 7 6) 5 (4 3) 2 1)




"kazzarazza003" <········@students.unisa.edu.au> wrote in message
·····································@localhost.talkaboutprogramming.com...
> Hi, I don't know how to reverse a list within a list properly (e.g (a (b c
> d)) = ((d c b) a). Instead it comes out as ((b c d) a)
>
> My code is:
>
> (defun reverse1 (list)
> (cond ((null (car list)) nil)
> (t (append (reverse1 (cdr list))
> (list (car list))))))
>
> I am guessing that there needs to be another condition but I don't know
> how to say if the list is within a list. I am new to lisp. Please help!
>