reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 1)

This is a discussion on reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 1) within the lisp forums in Programming Languages category; my reverse: (defun rvrs (lista) (if (null lista) '() (cons (rvrs (cdr lista)) (car lista)))) CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 1) built-in: CL-USER> (reverse '(1 2 3)) (3 2 1) So my reverse gets the spirit right but doesnt deliver exactly what I want. How do I cons without adding dots?...

Go Back   Application Development Forum > Programming Languages > lisp

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-23-2008, 12:25 PM
ssecorp
Guest
 
Default reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 1)

my reverse:
(defun rvrs (lista)
(if (null lista)
'()
(cons (rvrs (cdr lista)) (car lista))))

CL-USER> (rvrs '(1 2 3))
(((NIL . 3) . 2) . 1)

built-in:
CL-USER> (reverse '(1 2 3))
(3 2 1)

So my reverse gets the spirit right but doesnt deliver exactly what I
want. How do I cons without adding dots?
Reply With Quote
  #2  
Old 08-23-2008, 12:55 PM
Kenny
Guest
 
Default Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2). 1)

ssecorp wrote:
> my reverse:
> (defun rvrs (lista)
> (if (null lista)
> '()
> (cons (rvrs (cdr lista)) (car lista))))
>
> CL-USER> (rvrs '(1 2 3))
> (((NIL . 3) . 2) . 1)
>
> built-in:
> CL-USER> (reverse '(1 2 3))
> (3 2 1)
>
> So my reverse gets the spirit right but doesnt deliver exactly what I
> want. How do I cons without adding dots?


Do you know what a cons is, and how a proper list is defined in terms of
conses?

kt
Reply With Quote
  #3  
Old 08-23-2008, 01:07 PM
Barry Margolin
Guest
 
Default Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 1)

In article
<186b2923-9d79-4385-bdfe-d3b6642dee04@t54g2000hsg.googlegroups.com>,
ssecorp <circularfunc@gmail.com> wrote:

> my reverse:
> (defun rvrs (lista)
> (if (null lista)
> '()
> (cons (rvrs (cdr lista)) (car lista))))
>
> CL-USER> (rvrs '(1 2 3))
> (((NIL . 3) . 2) . 1)
>
> built-in:
> CL-USER> (reverse '(1 2 3))
> (3 2 1)
>
> So my reverse gets the spirit right but doesnt deliver exactly what I
> want. How do I cons without adding dots?


Hint: The dot goes away when the cdr is another list. But in your case,
the cdr is just a single element (the car of the original list).

Look at the simplest case:

(rvrs '(1)) == (cons (rvrs '()) 1) == (cons '() 1)

Work on fixing this case, and I think the rest should fall into place.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Reply With Quote
  #4  
Old 08-23-2008, 01:39 PM
Frank Buss
Guest
 
Default Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 1)

ssecorp wrote:

> my reverse:
> (defun rvrs (lista)
> (if (null lista)
> '()
> (cons (rvrs (cdr lista)) (car lista))))
>
> CL-USER> (rvrs '(1 2 3))
> (((NIL . 3) . 2) . 1)
>
> built-in:
> CL-USER> (reverse '(1 2 3))
> (3 2 1)
>
> So my reverse gets the spirit right but doesnt deliver exactly what I
> want. How do I cons without adding dots?


One idea would be to implement (a slightly modified) useful Haskell
function like this one:

(defun foldl (fun id list)
(if list
(foldl fun
(funcall fun (car list) id)
(cdr list))
id))

The idea of this function is to calculate for a list (1 2 3) the result
(fun 3 (fun 2 (fun 1 id)))

Now your rvrs function can be written like this:

(defun rvrs (list)
(foldl #'cons nil list))

With foldl some other nice functions are easy, e.g.:

(defun sum (list)
(foldl #'+ 0 list))

Of course, Common Lisp doesn't guarantee to be tail recursive, so for long
lists you would use something like this:

(defun rvrs (list)
(loop for i in list
with result = nil
do (push i result)
finally (return result)))

And for sum you would just write (reduce #'+ list).

--
Frank Buss, fb@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Reply With Quote
  #5  
Old 08-23-2008, 01:56 PM
ssecorp
Guest
 
Default Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) .1)

On Aug 23, 6:55*pm, Kenny <kentil...@gmail.com> wrote:
> ssecorp wrote:
> > my reverse:
> > (defun rvrs (lista)
> > * (if (null lista)
> > * * * '()
> > * * * (cons (rvrs (cdr lista)) (car lista))))

>
> > CL-USER> (rvrs '(1 2 3))
> > (((NIL . 3) . 2) . 1)

>
> > built-in:
> > CL-USER> (reverse '(1 2 3))
> > (3 2 1)

>
> > So my reverse gets the spirit right but doesnt deliver exactly what I
> > want. How do I cons without adding dots?

>
> Do you know what a cons is, and how a proper list is defined in terms of
> conses?
>
> kt


yes so I tried:

(defun rvrs (lista)
(if (null lista)
'()
(list (car (rvrs (cdr lista))) (car lista))))

(defun rvrs (lista)
(if (null lista)
'()
(list (rvrs (cdr lista)) (car lista))))
Reply With Quote
  #6  
Old 08-23-2008, 02:15 PM
ssecorp
Guest
 
Default Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) .1)

thanks frank I see know.

but Im still trying to figure out how to do it with just one simple
reucrsive function just as an exercise.

(defun rvrs (lista)
(if (null lista)
'()
(cons (rvrs (cdr lista)) (list (car lista)))))

CL-USER> (rvrs '(1 2 3))
(((NIL 3) 2) 1)
Reply With Quote
  #7  
Old 08-23-2008, 02:37 PM
ssecorp
Guest
 
Default Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) .1)

can I accumulate a list with loop?

like so:
CL-USER> (mapcar (lambda (x)(* x x)) (range 1 10 2))
(1 9 25 49 81)


with loop?
CL-USER> (loop for i in (range 1 10 2) do (print (* i i i)))


1
27
125
343
729
NIL

(loop for i in (range 1 10 2) do (* i i i))
NIL

Reply With Quote
  #8  
Old 08-23-2008, 02:57 PM
Frank Buss
Guest
 
Default Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 1)

ssecorp wrote:

> can I accumulate a list with loop?
>
> like so:
> CL-USER> (mapcar (lambda (x)(* x x)) (range 1 10 2))
> (1 9 25 49 81)
>
>
> with loop?
> CL-USER> (loop for i in (range 1 10 2) do (print (* i i i)))


I don't know range, but assume it is something like this:

(defun range (start emd step)
(loop for i from start to end by step collect i))

And this is the answer to your question, if you can accumulate with loop.

--
Frank Buss, fb@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Reply With Quote
  #9  
Old 08-23-2008, 03:02 PM
Pascal J. Bourguignon
Guest
 
Default Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 1)

ssecorp <circularfunc@gmail.com> writes:

> thanks frank I see know.
>
> but Im still trying to figure out how to do it with just one simple
> reucrsive function just as an exercise.
>
> (defun rvrs (lista)
> (if (null lista)
> '()
> (cons (rvrs (cdr lista)) (list (car lista)))))


The error is that you call CONS with a list instead of with an element
as the first argument.

A list is either the symbol NIL, representing the empty list (),
or a CONS cell containing an element, followed a LIST containing the rest.

LIST --> NIL
LIST --> (CONS element LIST)

If you try to use CONS otherwise, you won't get a LIST, but a
different data stucture, like a TREE or whatever.

> CL-USER> (rvrs '(1 2 3))
> (((NIL 3) 2) 1)


--
__Pascal Bourguignon__ http://www.informatimago.com/
Kitty like plastic.
Confuses for litter box.
Don't leave tarp around.
Reply With Quote
  #10  
Old 08-23-2008, 03:06 PM
Frank Buss
Guest
 
Default Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 1)

ssecorp wrote:

> thanks frank I see know.
>
> but Im still trying to figure out how to do it with just one simple
> reucrsive function just as an exercise.
>
> (defun rvrs (lista)
> (if (null lista)
> '()
> (cons (rvrs (cdr lista)) (list (car lista)))))
>
> CL-USER> (rvrs '(1 2 3))
> (((NIL 3) 2) 1)


Maybe you should learn a bit more about the basic list operations. Some
intersting examples for your problem:

(cons 1 (cons 2 (list 3)))
(list 2 3)
(append (list 1) (cons 2 nil))

Please try to say the results without testing it in the REPL. If you fully
understand how lists work in Lisp, it should be no problem for you to fix
the rvrs function.

--
Frank Buss, fb@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:01 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.