Reputation: 4273
I'm doing some homework in Lisp, using clisp to test, and I'm loading this code and running in in clisp
(defun myreverse (thelist)
(reverse thelist)
(print thelist)
(if (equal thelist nil)
nil
(if (consp (first thelist))
(cons (myreverse (reverse (first thelist)))
(myreverse (reverse (rest thelist))))
(cons (first thelist) (myreverse (rest thelist))))))
I'm kind of new to Lisp, but this code isn't reversing thelist
at all, my output is:
[18]> (myreverse '(a (b c) d))
(A (B C) D)
((B C) D)
(C B)
(B)
NIL
(D)
NIL
(A (C B) D)
The first line of my code says (reverse thelist)
, why isn't it reversing for the first print statement? Am I missing something?
Upvotes: 3
Views: 382
Reputation: 20229
I believe (reverse)
is without side-effects, thus it doesn't reverse the original list, but returns a new, reversed one. This is not so natural in Common Lisp, but expected in Scheme. Nevertheless, here is the doc http://www.lispworks.com/documentation/HyperSpec/Body/f_revers.htm#reverse
What I think you want is (nreverse)
.
Upvotes: 4