From: ssecorp
Subject: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 1)
Date: 
Message-ID: <186b2923-9d79-4385-bdfe-d3b6642dee04@t54g2000hsg.googlegroups.com>
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?

From: Kenny
Subject: Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 1)
Date: 
Message-ID: <48b04102$0$29513$607ed4bc@cv.net>
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
From: ssecorp
Subject: Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 	1)
Date: 
Message-ID: <c3276fef-cea9-4a16-8f0c-9a9f5001ca99@l42g2000hsc.googlegroups.com>
On Aug 23, 6:55 pm, Kenny <·········@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))))
From: ssecorp
Subject: Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 	1)
Date: 
Message-ID: <b166e67a-6364-4e4a-95c2-33dc3797fc58@34g2000hsh.googlegroups.com>
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)
From: ssecorp
Subject: Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 	1)
Date: 
Message-ID: <0be33bcb-2dde-4484-a53c-b9275026aa19@a1g2000hsb.googlegroups.com>
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
From: Frank Buss
Subject: Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) .  1)
Date: 
Message-ID: <csmggfeexuhc$.f4rhmrdjucge.dlg@40tude.net>
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, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Pascal J. Bourguignon
Subject: Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) .  1)
Date: 
Message-ID: <87abf3wo4p.fsf@hubble.informatimago.com>
ssecorp <············@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.
From: Frank Buss
Subject: Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) .  1)
Date: 
Message-ID: <ua2q2op7yoq5$.16cbgph0kkmz1$.dlg@40tude.net>
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, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Pascal J. Bourguignon
Subject: Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) .  1)
Date: 
Message-ID: <8763prwlz9.fsf@hubble.informatimago.com>
ssecorp <············@gmail.com> writes:
> but Im still trying to figure out how to do it with just one simple
> reucrsive function just as an exercise.

Doing it with a simple recursive function, you will either need a lot
of time, or a little more space.


Assume you want to reverse list = (a b c d e)

(first list) = a
(rest  list) = (b c d e)

(rev (rest list)) = (e b c d)

which is almost what you want, only with a appended.  If you use
(nconc (rev (rest list)) (list (first list))) you will spend O(N^2)
time walking the reversed sublists to add the last element.  And if
you use append instead of nconc, you will allocate also O(N^2)
temporary cons cells.


So let's do otherwise. Instead of taking the reverse of the rest, you
could remark that
   (rev (list (first list))) == (list (first list)) == (a)
and that you can build (b a), the reverse of list = (a b) 
with (cons (first (rest list)) (list (first list)))

Then you can build the reverse of the list by taking the first
element, and cons it to the resulting, reversed list.  You need two
parameters:

(defun rev (list reversed) ...)

(rev '(a b c) '()) --> (c b a)



-- 
__Pascal_Bourguignon__               _  Software patents are endangering
()  ASCII ribbon against html email (o_ the computer industry all around
/\  1962:DO20I=1.100                //\ the world http://lpf.ai.mit.edu/
    2001:my($f)=`fortune`;          V_/   http://petition.eurolinux.org/
From: Kenny
Subject: Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) .  1)
Date: 
Message-ID: <48b063b2$0$7322$607ed4bc@cv.net>
ssecorp wrote:
> On Aug 23, 6:55 pm, Kenny <·········@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

no.

Show us how to create the list (1 2) using only cons.

then take that list and the list (3 4) and create (1 2 3 4) using only 
cdr and setf. it's ok if the solution works only if the first list is 
length 2.

kt
From: Barry Margolin
Subject: Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 1)
Date: 
Message-ID: <barmar-F25190.13075223082008@newsgroups.comcast.net>
In article 
<····································@t54g2000hsg.googlegroups.com>,
 ssecorp <············@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, ······@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 ***
From: Frank Buss
Subject: Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 1)
Date: 
Message-ID: <12gp6bgi1a0zr$.tfebivh3tkwl$.dlg@40tude.net>
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, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Madhu
Subject: Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 1)
Date: 
Message-ID: <m3k5e1wutb.fsf@moon.robolove.meer.net>
* Frank Buss <································@40tude.net> :
Wrote on Sat, 23 Aug 2008 19:39:57 +0200:

| 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))
|
| (defun rvrs (list)
|   (foldl #'cons nil list))

[SNIP]

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

I hope you aren't forgetting REDUCE (with :FROM-END argument) _is_
FOLDL/FOLDR.

I think your definition of FOLDL could be written as

(defun foldl (fun id list)
  (reduce (lambda (x y) (funcall fun y x)) list :initial-value id))

without worrying about tail recursion

I notice I sometimes do not want to use `FOLDL' or `FOLDR' explicitly
when I can just use the REDUCE expression: The rationale is the same as
using anonymous functions i.e. using a LAMBDA instead of naming that
abstraction.

--
Madhu
From: ardaliev
Subject: Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 	1)
Date: 
Message-ID: <36b6879c-ee6d-4db6-af22-2843b5508c16@p25g2000hsf.googlegroups.com>
On Aug 23, 6:25 pm, ssecorp <············@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?



( defun my-reverse ( list)
	   (if ( null list)
	       nil
	       (append ( my-reverse (  cdr list)) (list ( car list)))))
Not very elegant, but it works!!
From: Kent M Pitman
Subject: Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) .  1)
Date: 
Message-ID: <u7ia6758j.fsf@nhplace.com>
ardaliev <········@gmail.com> writes:

> On Aug 23, 6:25 pm, ssecorp <············@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?

These first remarks are to ssecorp:

The dots are not really there.  They are visual artifact, not something in 
your code.  All lists have them, but they are almost always not shown to 
you.  That is,

 (a . (b . (c . ())))

is the same as (a b c), the printer just doesn't show you.  But the dot is
there.  What the dot tells you, when you see it appear, is that none of the
shorthand rules for eliding the dot applies.  Since all of the shorthanding
rules rely on there being a proper list to the right of the dot, what this
tells you is that you've built a cons that is not a proper list; that is,
it is not a list whose righthand backbone ultimately terminates in an
empty list.  And since the reverse of a proper list is a proper list, what
you're really learning is that you have not correctly constructed either the
base case [which is fine here] or the inductive step.

So consider your inductive step:

  (cons (rvrs (cdr lista)) (car lista)) 

What this implies is that there is some list

  ( first . rest )

such that 

  ( rest . first )

is its reverse.  And that assumption is false.  The reverse of a right-handed
list is not a left-handed list.  It is a right-handed list that has its 
elements in the other order.  That is,

  (1 2 3)

which is the list

  (1 . (2 . (3 . ())))

must reverse to

  (3 . (2 . (1 . ())))

not to

  (((() . 3) . 2) . 1)

So you must write the recursion differently.  I'm not going to do that
for you.  But hopefully seeing that the dot is not the problem but rather
your understanding of the structure to be achieved is the problem, you'll
get there.

Getting back to ardaliev's reply:

> ( defun my-reverse ( list)
> 	   (if ( null list)
> 	       nil
> 	       (append ( my-reverse (  cdr list)) (list ( car list)))))
> Not very elegant, but it works!!

It's partly not elegant because of the spaces between th open parens
and the operators.  That's a superficial matter, but you should definitely
avoid this.  There are many variations of good Lisp style, but there is 
almost no reason for ever doing this. [I could cite exceptions.  Nearly all
style rules have exceptions, after all.   But I know of no exceptions that
would apply here.]

The really inelegant part is the call to APPEND.  APPEND is an O(n),
not an O(1) function.  In sloppy terms, it means it has to do work
proportional to the number of elements in the list it's appending to.
That is, if you're adding one element to the end of an n-length list
inside a loop from 0 to n, then APPEND will get called, on average n/2
(the average length of the list) times n (the number of times you
looped) or 1/2 n^2, which we notate as just O(n^2) ignoring the constant
factor of 1/2 because as the problem goes, the dominant bit of interest
is the exponent, not the constant factor.

Anything O(n^2) can be trouble even if n gets only moderately large.
You're better off sticking to CONS when you're in a loop, since it is
O(1).  That doesn't mean you can just substitute CONS for APPEND and
be done with it--it's not the same function, and so what you have to
do with CONS is different.  But it does mean when you see APPEND in a
loop [or in a recursion] in your code, you should always be nervous
and double-checking your algorithmic complexity VERY carefully.

So the speed issue is the true inelegance of what you offer, and in
truth although what you have is probably functionally correct (returns
the intended answer--I didn't try it, actually, but it looks
plausible), it's a major hurdle in the learning of Lisp that you must
not accept this as correct because there is more to a program,
especially a very general subroutine like a REVERSE function, than
functional correctness.  If you tolerate algorithmic inefficiency,
your programs (even when compiled with heavy optimization) will run
very slowly when they get to even intermediate size, and you'll
probably blame the language rather than your own coding.  (That's
convenient if you're in the kind of verbal debate where people don't
do much fact-checking and want to believe an unfounded claim that Lisp
is slow anyway; you'll probably find many people rushing to agree with
you because they want to believe that Lisp is slow and they applaud
you for 'proving' it.  But it's less good if you actually care about
what the truth of why your program runs slowly is.)
From: Tamas K Papp
Subject: Re: reverse not working: CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 1)
Date: 
Message-ID: <6hch9mFjv3ivU3@mid.individual.net>
On Sat, 23 Aug 2008 09:25:34 -0700, ssecorp wrote:

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

Please do yourself a favor and read (up to) Chapter 3 of Graham's ANSI 
Common Lisp.

Tamas