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!
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
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-.
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
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!
>