From: Edward Huang
Subject: I wrote a function to rotate ...
Date: 
Message-ID: <19297054.0311202052.39e070b6@posting.google.com>
Hi, 
I wrote a function to rotate a list.

   (defun rotate-left (a-list)
          (append (cdr a-list) (car a-list)))

When I typed:

   (rotate-left '(1 2 3 4 5))

I got a list (2 3 4 5 . 1).
Then I got an error while I try to rotate-left
twice: (rotate-left (rotate-left '(1 2 3))).
How is the dot produced?
I used CLISP v2.31.

From: cr88192
Subject: Re: I wrote a function to rotate ...
Date: 
Message-ID: <vrr7o9rme9ha84@corp.supernews.com>
"Edward Huang" <········@ms5.pccu.edu.tw> wrote in message
·································@posting.google.com...
> Hi,
> I wrote a function to rotate a list.
>
>    (defun rotate-left (a-list)
>           (append (cdr a-list) (car a-list)))
>
> When I typed:
>
>    (rotate-left '(1 2 3 4 5))
>
> I got a list (2 3 4 5 . 1).
> Then I got an error while I try to rotate-left
> twice: (rotate-left (rotate-left '(1 2 3))).
> How is the dot produced?
> I used CLISP v2.31.

list mechanics, dude...

lists are composed as a chain of cons cells, car is the value in the cell,
cdr is the next cell.
normally a list is terminated by an empty list ().
the dot tells that the last cdr contains a value other than an empty list.

so, implicitly: (1 2 3 4 5) is actually: (1 2 3 4 5 . ())
or, conversely: (1 . (2 . (3 . (4 . (5 . ()))))).

append appends 2 lists, and in the case before you are appending a list and
a number, so the number ends up in cdr.

thus:
(defun rotate-left (a-list)
      (append (cdr a-list) (cons (car a-list) '())))

should work better...
From: Fred Gilham
Subject: Re: I wrote a function to rotate ...
Date: 
Message-ID: <u7he0xsp5k.fsf@snapdragon.csl.sri.com>
> thus:
> (defun rotate-left (a-list)
>       (append (cdr a-list) (cons (car a-list) '())))
> 
> should work better...

Or, simply

(defun rotate-left (a-list)
  (append (cdr a-list) (list (car a-list))))

-- 
Fred Gilham                                        ······@csl.sri.com
The spam folder --- you will never find a more wretched hive of scum
and villainy.  We must be cautious.
From: cr88192
Subject: Re: I wrote a function to rotate ...
Date: 
Message-ID: <vrshccjd975l2d@corp.supernews.com>
"Fred Gilham" <······@snapdragon.csl.sri.com> wrote in message
···················@snapdragon.csl.sri.com...
>
> > thus:
> > (defun rotate-left (a-list)
> >       (append (cdr a-list) (cons (car a-list) '())))
> >
> > should work better...
>
> Or, simply
>
> (defun rotate-left (a-list)
>   (append (cdr a-list) (list (car a-list))))
>
yes, a little simpler...

oddly enough I more naturally thought of "cons" than "list".
it may just be that I work in c too much (dealing with the c-side interface
of my lang...), where I have a convinient macro for "cons" but little for
"list".
or it may be that I just think of the cons cells too much...

CONS(FIXNUM(1),
    CONS(FIXNUM(2),
        CONS(FIXNUM(3),
            CONS(FIXNUM(4),
                CONS(FIXNUM(5), MISC_EOL)))))

things are very much different when one has a compiler for a lang, suddenly,
one can actually interact with things (eg: opengl, generic c code, ...) more
directly, without having to write hoards of interface code (yay for ffi, yay
for being able to have inline c, ...).
all I have at present is static compilation and eval. there is at present no
means to dynamically compile stuff.

less cool, the general "pain in the 4ss" factor goes up a bit, eg: I have to
manage my code in the makefiles and such, and have to deal with "module
dependencies...".

ok, enough on that...
From: Kenneth P. Turvey
Subject: Re: I wrote a function to rotate ...
Date: 
Message-ID: <slrnbrra92.k5o.kt@premium.geo.yahoo.akadns.net>
On 20 Nov 2003 20:52:08 -0800, Edward Huang <········@ms5.pccu.edu.tw> wrote:
> Hi, 
> I wrote a function to rotate a list.
> 
>    (defun rotate-left (a-list)
>           (append (cdr a-list) (car a-list)))

This isn't quite right:

        (defun rotate-left (a-list)
                (append (cdr a-list) (list (car a-list))))

> When I typed:
> 
>    (rotate-left '(1 2 3 4 5))
> 
> I got a list (2 3 4 5 . 1).

This is interesting.  I guess it just sticks a reference to the value
of the CDR into the second half of the last cons cell.  This works for
lists, but not atoms.  I'm surprised it didn't complain.  I guess I
can see situations where this might be what you want.

-- 
Kenneth P. Turvey <··@squeakydolphin.com>

  Artificial Intelligence Algorithms Wiki 
  http://ai.squeakydolphin.com
From: Peter Seibel
Subject: Re: I wrote a function to rotate ...
Date: 
Message-ID: <m3u14yb924.fsf@javamonkey.com>
········@ms5.pccu.edu.tw (Edward Huang) writes:

> Hi, 
> I wrote a function to rotate a list.
> 
>    (defun rotate-left (a-list)
>           (append (cdr a-list) (car a-list)))
> 
> When I typed:
> 
>    (rotate-left '(1 2 3 4 5))
> 
> I got a list (2 3 4 5 . 1).
> Then I got an error while I try to rotate-left
> twice: (rotate-left (rotate-left '(1 2 3))).
> How is the dot produced?

Because (car a-list) is not a list. Consider the difference between:

 (list 1 2)  ==> (1 2)

 (cons 1 2)  ==> (1 . 2)

And the error is presumably because all the arguments to the last to
APPEND must be "proper" lists and your ROTATE-LEFT returns a "dotted"
list.

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp
From: Kenny Tilton
Subject: Re: I wrote a function to rotate ...
Date: 
Message-ID: <2ohvb.125269$Gq.17270411@twister.nyc.rr.com>
Edward Huang wrote:

> Hi, 
> I wrote a function to rotate a list.
> 
>    (defun rotate-left (a-list)
>           (append (cdr a-list) (car a-list)))
> 
> When I typed:
> 
>    (rotate-left '(1 2 3 4 5))
> 
> I got a list (2 3 4 5 . 1).
> Then I got an error while I try to rotate-left
> twice: (rotate-left (rotate-left '(1 2 3))).
> How is the dot produced?

The question may be your problem. "the dot" is the printers way of 
telling you that the CDR of the last cons is not another cons (as is 
normally the case in a well-formed Lisp list. "the dot" means that the 
cdr of the last cond cell points to the number 1 itself.

What you want is (abbreviated):

[5 v]
   [1 nil]

What you have is: [5 1]

others will provide better ascii art. as for how you did it, append 
expects lists, and your second argument to it was the number 1. Note 
that append does not really "expect lists" since it happily did your 
bidding, so let's just say, to get what you expected, give it all lists.

kt


-- 
http://tilton-technology.com

Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film

Your Project Here! http://alu.cliki.net/Industry%20Application